1
vote
2answers
137 views

Order a query by the sum of two “has_many” sub-tables?

In my app, Invoice has_many item_numbers and Invoice has_many payments. Each invoice has a balance, which is the sum of the ItemNumber amount attributes, less the sum of the Payment amount attributes. ...
0
votes
1answer
27 views

How to trim database column in SQL comparison for rails 3.2.12?

Please look at the following example: UserAccess.where("action = ? AND resource = ? ", 'index', 'logs') The issue we are facing is that there are white space in column action and resource in ...
1
vote
1answer
34 views

indexing multi-column index with Rails / Postgresql

I have a doubt about correct indexing. I use rails 3.2.13, using Posgresql behind. BTW, Probably more a relational databases/indexing question... I have this table: # Table name: exams # # id ...
0
votes
1answer
40 views

Optimizing slow query that uses concat in the order by clause

I'm having difficulties on a query that does several joins, and is very slow for large recordsets. Here is the query: SELECT order_headers.*, SUM(order_details.cost) AS amount, ...
0
votes
1answer
42 views
+100

PgSQL - to_tsquery and JOINS leads to Error in Rails

I have the following query: query = param.tag_list.join("|") title = "ts_headline(title, query) AS title" rank = "ts_rank_cd(tsv, query) AS rank" Job.select( [ title, rank, :starts_at, ...
1
vote
2answers
2k views

help understanding the fql query for mutual friends

Need to get the name and picture of mutual friends without using JS. Found FQL query below for mutual friends: "SELECT uid1 FROM friend WHERE uid2=[targetID] AND uid1 IN (SELECT uid2 FROM friend ...
3
votes
1answer
165 views

ActiveRecord query with includes and conditions not giving expected SQL

Ok, so here's the issue I'm having. I have a model that has two relationships set on conditions in a through table. has_one :link_resource, through: :resource_contexts, source: :resource, conditions: ...
0
votes
1answer
27 views

Rails includes with conditions

Is is possible in Rails > 3.2 to add conditions to the join statement generated by the includes method? Let's say I have two models, Person and Note. Each person has many notes and each note belong ...
2
votes
1answer
29 views

Turn SQL query into ActiveRecord Relation

How can I turn the following SQL query into an ActiveRecord relation so that I can expand on it with scopes? WITH joined_table AS ( SELECT workout_sets.weight AS weight, workouts.user_id ...
0
votes
3answers
40 views

Why does my users table have a no column error?

I am getting the following error after running my tests in the console: ActiveRecord::StatementInvalid: SQLite3::SQLException: table users has no column named password: INSERT INTO "users" ...
1
vote
1answer
50 views

Filtering model with HABTM relationship

I have 2 models - Restaurant and Feature. They are connected via has_and_belongs_to_many relationship. The gist of it is that you have restaurants with many features like delivery, pizza, sandwiches, ...
1
vote
2answers
464 views

rails raw sql example

How can I convert this code to raw sql and use in rails? Because When I deploy this code in heroku,there is a request timeout error.I think this will be faster if I use raw sql. @payments = ...
1
vote
2answers
30 views

Rails, ActiveRecord, complex query with multiple joins on nested models

I've been trying to create this complex query for some time now to no avail through ActiveRecord. I'm trying to get the workouts that have the highest workout_set.weight for the specified user and ...
4
votes
1answer
43 views

Tricky Rails ActiveRecord (SQL is acceptable!) query to find a proportion of users given conditions in two join tables

class User < ActiveRecord::Base has_many :views has_many :rates ... end class Resource < ActiveRecord::Base has_many :views has_many :rates ... end I am researching whether the ...
1
vote
1answer
69 views

how “cancan” gem process depth nested resource ability?

here is my ability define: can :manage,Order,:store => {:business_district => {:district_manager_id => user.id}} and when I loading resourses by: Order.accessible_by(current_ability) It ...

1 2 3 4 5 94
15 30 50 per page