2012-07-04 45 views
0

我有以下模式:Rails 3中加入2 belongs_to的關聯的表

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(:店).merge(Shop.europe)

我得到的錯誤: 的ActiveRecord :: ConfigurationError:協會命名爲 '區域' 的不發現;也許你拼錯了嗎?

有關我如何使這項工作的任何想法?

+0

你的表名在'locales'中,但類是'locale',還是它是一些ruby魔法添加s? –

回答

0
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)