2017-02-07 7 views
0

has_many :throughhas_oneの関係を使用するモデルを作成しようとしています。has_oneとの関係でhas_oneを使用する方法

class Cart < ActiveRecord::Base 
    has_many :users 
    has_many :bills, through: :users # I expect cart has many bills 
end 

class User < ActiveRecord::Base 
    has_one :bill 
    belongs_to :cart 
end 

class Bill < ActiveRecord::Base 
belongs_to :cart 
end 

私はcart.billsを呼び出すしようとすると、それはより多くの法案を持っているにもかかわらず、最初の法案を返します。

誰でも手助けできますか?

答えて

0

これはユーザーモデルの宣言のため、1つのレコードのみを取得しているという理由で、そのユーザーには1つの請求書しか持たないように指定しました。 正しいものは次のようになります。あなたはこれを参照することができます

class User < ActiveRecord::Base 
has_many :bills 
belongs_to :cart 
end 

DOC

レール
関連する問題