Rails join two tables based on relation in third - ruby-on-rails

I want to join 2 tables, but I need to use 3th to set a relation between them.
Each Order is prepared by an Employee. I want to find OrderDetails only for specific employees. To check that, I need to find who was assigned to shop orders.
I'm almost sure that I know how to do it in SQL. Just concept query:
SELECT OrderDetails.payment_id FROM OrderDetais
INNER JOIN ShopOrders ON ShopOrders.id = OrderDetails.shop_order_id
INNER JOIN Employees ON Employees.id = ShopOrders.employee_id
WHERE Employees.id IN (5, 6, 8)
I'm just trying to write the same in Rails:
query = OrderDetail
.where('order_details.created_at > ? AND order_details.created_at < ?', 1.year.ago, 2.days.ago)
query = query.joins(:shop_order)
I use to_sql to check if my query looks good. Everything goes ok, until I join query.joins(: employee)
query = OrderDetail
.where('order_details.created_at > ? AND order_details.created_at < ?', 1.year.ago, 2.days.ago)
query = query.joins(:shop_order)
query = query.joins(:employee)
then I receive an error:
ActiveRecord::ConfigurationError: Can't join 'OrderDetail' to association named 'employee'; perhaps you misspelled it?>
I tried to follow Joining Nested Associations (Multiple Level). But I don't know how to use it properly.
In OrderDetail model there is:
delegate :employee, to: :shop_order, allow_nil: true
In ShopOrder model:
belongs_to :employee, optional: false
has_many :order_details, dependent: :restrict_with_error
And Employee model:
has_many :shop_orders
has_many :order_details, through: :shop_orders

I fixed it like that:
query.joins(:shop_order)
.joins('JOIN employees ON employees.id = shop_orders.employee_id')
.where(employees: { id: [5, 6, 8] })

Basing on the 'Joining Nested Associations (Multiple Level)' reference you provided the following should do what you want:
query
.joins(shop_orders: :employee)
.where(employees: { id: [5, 6, 8] })

Related

Find records where association didnt already exists

how to get only records that isn't associated in Ebook model? Simply, i want to offer only ebooks that user didnt already have. I tried much solutions, one of best is:
Magazine.left_joins(:ebooks).where(ebooks: { id: nil })
but i dont know where to specify user_id
model Magazine
has_many :ebooks
has_many :users, :through => :ebook
model Ebook
belongs_to :user
belongs_to :magazine
model User
has_many :ebooks
Im new in rails, sorry for stupid question maybe.
#Hass
thank you, but this generate sql like this:
SELECT "magazines".* FROM "magazines"
INNER JOIN "ebooks" ON "magazines"."id" = "ebooks"."magazine_id"
LEFT OUTER JOIN "ebooks" "ebooks_magazines" ON "ebooks_magazines"."magazine_id" = "magazines"."id"
WHERE "ebooks"."user_id" = 1 AND "ebooks"."id" IS NULL
(WHERE "ebooks"."user_id" = 1 => this returns 0 records, because where filter doesnt find any row in table ebooks)
but i need something like this:
SELECT "magazines".*, ebooks.user_id FROM "magazines"
LEFT JOIN "ebooks" ON "magazines"."id" = "ebooks"."magazine_id" AND ebooks.user_id = 1
LEFT OUTER JOIN "ebooks" "ebooks_magazines" ON "ebooks_magazines"."magazine_id" = "magazines"."id"
WHERE ebooks.user_id IS NULL
this returns rows that im looking for but i dont know how to do this with rais and associations
You want to use a left_outer_join instead.
Magazine.left_outer_joins(:ebooks).where(ebooks: { id: nil })

Find all parents where all child record matches exactly from given list in has many through association in Rails

I have following relationship
class Invoice
has_many :entries
has_many :price_matrixes, through: entries
end
class Entry
belongs_to :invoice
belongs_to :price_matrix
end
class PriceMatrix
has_many :entries
has_many :invoices, through: entries
end
Now, I have a set of price_matrix ids, for example [1, 2] and, I want to find all invoices that have EXACTLY this two price matrix only.
Note: Tried this Invoice.includes(:price_matrixes).where(price_matrixes: {id: [1, 2]}) but this is supposed to return record even if one of the ids from list will match.
The task can be solved easily with plain SQL. Assuming you use PostgreSQL 9+, this is SQL-query to fetch invoice ids which match your condition (have EXACTLY price matrixes with ids [1, 2] only):
SELECT I.id
FROM invoices I
JOIN entries E ON E.invoice_id = I.id
GROUP BY I.id
HAVING array_agg(E.price_matrix_id ORDER BY E.price_matrix_id ASC) = ARRAY[1,2];
Here we join invoices and entries, group results by invoice.id, and filter only those ones which have given price_matrix_ids. Please note that ARRAY[1,2] expression should contain price_matrix_ids sorted in ascending order.
Online demo: http://rextester.com/TAEUU9220
Back to Ruby, here is the code:
price_matrix_ids = [1,2].sort
Invoice.joins(:entries).having("array_agg(entries.price_matrix_id ORDER BY entries.price_matrix_id ASC) = ARRAY[#{price_matrix_ids.join(',')}]").group('invoices.id')
Rails >= 3.2
Invoice.includes(:price_matrix).where(price_matrix: {id: [1, 2]})
Rails < 3.2
Invoice.joins(:price_matrix).where("price_matrix.id IN [1, 2]")
You can use an inner join and the same where clause you tried.
Invoice
.joins('INNER JOIN entries on entries.invoice_id = invoices.id')
.where(entries: { price_matrix_id: [1, 2] })

How do I query with '.includes' across more than two tables?

I have the following associations:
class Captain
has_many :boats
end
class Boat
belongs_to :captain
has_many :classifications
end
class Classification
has_many :boats
end
I want to find out which captains have boats that have classifications with :name attributes of "catamaran."
This has been my best guess so far:
Captain.includes(:boats, :classifications).where(:boats => {:classifications => {:name => "catamaran"}})
Try this
Captain.joins(boats: :classifications).where(classifications: { name: "catamaran" })
This query results in following SQL query
SELECT * FROM `captains`
INNER JOIN `boats` ON `boats`.`captain_id` = `captains`.`id`
INNER JOIN `join_table` ON `join_table`.`boat_id` = `boat`.`id`
INNER JOIN `classifications` ON `join_table`.`classification_id` = `classifications`.id
#Sujan Adiga has right!
If you use the include method, active record generate 2 separates sql query. The first for your main Model, and the second for your inclued model. But you don't have access on the included model in your first query.
When you use the joins method, active record generate sql query with joins statement. So you can use the joined model in your clause where.

Rails ActiveRecord query using multiple joins involving polymorphic association

I'm trying to figure out how I can replicate the following SQL query using AR given the model definitions below. The cast is necessary to perform the average. The result set should group foo by bar (which comes from the polymorphic association). Any help is appreciated.
SQL:
SELECT AVG(CAST(r.foo AS decimal)) "Average", s.bar
FROM rotation r INNER JOIN cogs c ON r.cog_id = c.id
INNER JOIN sprockets s ON s.id = c.crankable_id
INNER JOIN machinists m ON r.machinist_id = m.id
WHERE c.crankable_type = 'Sprocket' AND
r.machine_id = 123 AND
m.shop_id = 1
GROUP BY s.bar
ActiveRecord Models:
class Rotation < ActiveRecord::Base
belongs_to :cog
belongs_to :machinist
belongs_to :machine
end
class Cog < ActiveRecord::Base
belongs_to :crankable, :polymorphic => true
has_many :rotation
end
class Sprocket < ActiveRecord::Base
has_many :cogs, :as => :crankable
end
class Machinist < ActiveRecord::Base
belongs_to :shop
end
UPDATE
I've figured out a way to make it work, but it feels like cheating. Is there are a better way than this?
Sprocket.joins('INNER JOIN cogs c ON c.crankable_id = sprockets.id',
'INNER JOIN rotations r ON r.cog_id = c.id',
'INNER JOIN machinists m ON r.machinist_id = m.id')
.select('sprockets.bar', 'r.foo')
.where(:r => {:machine_id => 123}, :m => {:shop_id => 1})
.group('sprockets.bar')
.average('CAST(r.foo AS decimal)')
SOLUTION
Albin's answer didn't work as-is, but did lead me to a working solution. First, I had a typo in Cog and had to change the relation from:
has_many :rotation
to the plural form:
has_many :rotations
With that in place, I am able to use the following query
Sprocket.joins(cogs: {rotations: :machinist})
.where({ machinists: { shop_id: 1 }, rotations: { machine_id: 123}})
.group(:bar)
.average('CAST(rotations.foo AS decimal)')
The only real difference is that I had to separate the where clause since a machine does not belong to a machinist. Thanks Albin!
I think this code is a little simpler and taking more help from AR
Sprocket
.joins(cogs: {rotations: :machinist})
.where({ machinists: { machine_id: 123, shop_id: 1 } } )
.group(:bar)
.average('CAST(rotations.foo AS decimal)')
The select clause was unnecessary, you don't have to select values since you only need them internally in the query, AR helps you decide what you need afterwards.
I tested this out using a similar structure in one of my own projects but it is not the exact same models so there might be a typo or something in there if it does not run straight up. I ran:
Activity
.joins(locations: {participants: :stuff})
.where({ stuffs: { my_field: 1 } })
.group(:title)
.average('CAST(participants.date_of_birth as decimal)')
producing this query
SELECT AVG(CAST(participants.date_of_birth as decimal)) AS average_cast_participants_date_of_birth_as_decimal, title AS title
FROM `activities`
INNER JOIN `locations` ON `locations`.`activity_id` = `activities`.`id`
INNER JOIN `participants` ON `participants`.`location_id` = `locations`.`id`
INNER JOIN `stuffs` ON `stuffs`.`id` = `participants`.`stuff_id`
WHERE `stuffs`.`my_field` = 1
GROUP BY title
which AR makes in to a hash looking like this:
{"dummy title"=>#<BigDecimal:7fe9fe44d3c0,'0.19652273E4',18(18)>, "stats test"=>nil}

ActiveRecord find categories which contain at least one item

Support I have two models for items and categories, in a many-to-many relation
class Item < ActiveRecord::Base
has_and_belongs_to_many :categories
class Category < ActiveRecord::Base
has_and_belongs_to_many :items
Now I want to filter out categories which contain at least one items, what will be the best way to do this?
I would like to echo #Delba's answer and expand on it because it's correct - what #huan son is suggesting with the count column is completely unnecessary, if you have your indexes set up correctly.
I would add that you probably want to use .uniq, as it's a many-to-many you only want DISTINCT categories to come back:
Category.joins(:items).uniq
Using the joins query will let you more easily work conditions into your count of items too, giving much more flexibility. For example you might not want to count items where enabled = false:
Category.joins(:items).where(:items => { :enabled => true }).uniq
This would generate the following SQL, using inner joins which are EXTREMELY fast:
SELECT `categories`.* FROM `categories` INNER JOIN `categories_items` ON `categories_items`.`category_id` = `categories`.`id` INNER JOIN `items` ON `items`.`id` = `categories_items`.`item_id` WHERE `items`.`enabled` = 1
Good luck,
Stu
Category.joins(:items)
More details here: http://guides.rubyonrails.org/active_record_querying.html#joining-tables
please notice, what the other guys answererd is NOT performant!
the most performant solution:
better to work with a counter_cache and save the items_count in the model!
scope :with_items, where("items_count > 0")
has_and_belongs_to_many :categories, :after_add=>:update_count, :after_remove=>:update_count
def update_count(category)
category.items_count = category.items.count
category.save
end
for normal "belongs_to" relation you just write
belongs_to :parent, :counter_cache=>true
and in the parent_model you have an field items_count (items is the pluralized has_many class name)
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
in a has_and_belongs_to_many relation you have to write it as your own as above
scope :has_item, where("#{table_name}.id IN (SELECT categories_items.category_id FROM categories_items")
This will return all categories which have an entry in the join table because, ostensibly, a category shouldn't have an entry there if it does not have an item. You could add a AND categories_items.item_id IS NOT NULL to the subselect condition just to be sure.
In case you're not aware, table_name is a method which returns the table name of ActiveRecord class calling it. In this case it would be "categories".

Resources