简体   繁体   中英

Rails: has_many :through

I am trying to figure out if my approach is proper. I have a User model and a Compensation Model. I need to allow the application admin to assign compensation to each User record. This compensation may change over time and I want to track the changes. After some research, I thought that the has_many => :through was the way to approach this using a third model called Payments to join the other two and inside that model have the user_id and compensation_id. I have the following set:

class User < ActiveRecord::Base

has_many :payments
has_many :compensations, :through => :payments


class Compensation < ActiveRecord::Base

has_many :payments
has_many :users, :through => :payments

class Payment < ActiveRecord::Base

has_many :Users
has_many :compensations

My first question is: Am I correct, that this would be the best approach knowing what I am trying to achieve? My second question is what is the simplest way to include a drop down field in the form (pooling from the compensation table) that would populate the Payment Table? Unfortunately, most web discussions I have reviewed about has_many :through centers on the models and not the view.

Any assistance would be appreciated.

class User < ActiveRecord::Base
 has_many :payments
 has_many :compensations, :through => :payments
end

class Compensation < ActiveRecord::Base
  has_many :payments
  has_many :users, :through => :payments
end

class Payment < ActiveRecord::Base
  belongs_to :user
  belongs_to :compensation
end

Try this may be it's helpful for you. More info. see this video - has_many->through

It's not really clear what your goal is. Why would a user have many compensations? Why would a compensation have many users, wouldn't any particular compensation be for a specific user being compensated? I'm also not sure what form you're talking about in your second question.

That said, assuming you want something that does something similar to your code above, you would just need:

class User < ActiveRecord::Base
  has_and_belongs_to_many :compensations
end

class Compensation < ActiveRecord::Base
  has_and_belongs_to_many :users
end

As for the dropdown, look into Rails select helpers. Sorry I can't be more specific, again, I'm not exactly sure what you're trying to do.

我已经弄清楚了-但朝另一个方向走-现在,我决定不能够跟踪历史记录,而是使用collection_select创建了动态下拉列表–

Your first question is: Am I correct, that this would be the best approach knowing what I am trying to achieve? Ans: This is good approach but you can "polymorphic associations" there for more specific solutions. Your second question is what is the simplest way to include a drop down field in the form (pooling from the compensation table) that would populate the Payment Table?

Second question is not much clear. Do you want to display value of some attribute?

If so, follow my this example. I think It'll help you.

contact.select :contactable, @member.contacts.map {|r| [r.first_name,r.id] }

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