如何在Ruby On Rails中设置:通过ActiveRecord关联?

时间:2016-01-11 21:07:16

标签: ruby-on-rails ruby activerecord

我无法找出关联。我需要通过交易将用户链接到反馈。反馈有transaction_id属性,Transaction有seller_id。我如何使用关联来做到这一点?这是我到目前为止,感谢您的帮助!谢谢!

class Feedback < ActiveRecord::Base
     belongs_to :user
end

class User < ActiveRecord::Base
    has_many :feedbacks
end

class Transaction < ActiveRecord::Base
    belongs_to :seller, :class_name => 'User', :foreign_key => 'seller_id'
end

1 个答案:

答案 0 :(得分:2)

用户 - &gt;交易 - &gt;反馈

class User < ActiveRecord::Base
    has_many :transactions
    has_many :feedbacks, through: :transactions, foreign_key: 'seller_id'
end

class Transaction < ActiveRecord::Base
    has_many :feedback
    belongs_to :seller, class_name: 'User', foreign_key: 'seller_id'
end

class Feedback < ActiveRecord::Base
     belongs_to :transaction
end