user1648020
user1648020

Reputation: 97

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.

Upvotes: 1

Views: 3102

Answers (4)

pratik
pratik

Reputation: 64

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] }

Upvotes: 0

user1648020
user1648020

Reputation: 97

I figured it out -- but went in another direction -- for now I have decided against being able to track history and created a dynamci drop down using collection_select –

Upvotes: 0

Dipak Panchal
Dipak Panchal

Reputation: 6036

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

Upvotes: 2

Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 18607

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.

Upvotes: 0

Related Questions