[英]Rails 3 joining 2 belongs_to associated tables
我有以下架构:
class Locale < ActiveRecord::Base
has_many :shops
# region : String
end
class Shop < ActiveRecord::Base
belongs_to :locale
has_many :carts
scope :europe, joins(:locale).where('locales.region = ?', 'Europe')
end
class Cart < ActiveRecord::Base
belongs_to :shop
scope :purchased, where('purchased_at is not null')
# purchased_at : DateTime
end
我想查找在某个地区购买的所有购物车,我有几个作用域设置,以使查询更具可读性,但是在尝试时:
Cart.purchased.join(:shop).merge(Shop.europe)
我收到错误消息:ActiveRecord :: ConfigurationError:未找到名为“ locale”的关联; 也许您拼错了?
关于如何进行这项工作有任何想法吗?
class Locale < ActiveRecord::Base
has_many :shops
scope :europe, where(region: 'Europe')
end
class Shop < ActiveRecord::Base
belongs_to :locale
has_many :carts
end
class Cart < ActiveRecord::Base
belongs_to :shop
scope :purchased, where('purchased_at is not null')
end
Cart.purchased.joins(shop: :locale).merge(Locale.europe)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.