I have following tables 4 models
class ItemCode < ActiveRecord::Base
belongs_to :item_point
end
class ItemPoint < ActiveRecord::Base
belongs_to :item
has_many :item_codes
end
class Item < ActiveRecord::Base
belongs_to :prodcut_category
has_many :item_points
end
class ProductCategory < ActiveRecord::Base
has_many :items
end
Now I have to find item_codes details using product_category for this I used inner join. Here is mysql query
SELECT *
FROM `item_codes` utc
INNER JOIN item_points rtp ON rtp.id = utc.item_point_id
INNER JOIN items ri ON ri.id = rtp.item_id
INNER JOIN product_catagories rpc ON rpc.id = ri.product_catagory_id
WHERE rpc.id =1
LIMIT 0 , 30
Now I have to write the same query in the Acitve record format.
UniqueItemCode.joins(:item_point).joins(:item).joins(:product_catagory).where("product_catagories.id = 1")
getting following error
ActiveRecord::ConfigurationError: Association named 'item_points' was not found; perhaps you misspelled it?
So how can write given query in the Active record format.
Aucun commentaire:
Enregistrer un commentaire