简体   繁体   中英

How do I set up a :through ActiveRecord association in Ruby On Rails?

I am having trouble figuring out associations. I need to link up a User to Feedback through Transactions. Feedback has transaction_id property and Transaction has seller_id. How would I do that using associations? Here is what I have so far, appreciate your help! Thanks!

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

User -> Transactions -> Feedback

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM