Has_many :through

I’m new in rails and I’ve some doubts about what kind of relationship do
I
have to use. Here the case.

I have two models Offer and User, a user could apply to many offers and
offers can have many user also the users create the offers.

I think I have to use a has_many :through ralationship. for example I
create another model “Applicant”, applicant belongs_to user and
belongs_to
offer. But how is the relationship from the user and offer model? for
example

User Model

has_many :offer, :through => :applicant

Offer Model

has_many :user, :through => :applicant

My doubt is because I already have this two relationship

User Model

has_many :offers, :dependent => :destroy

Offer Model

belongs_to :user

After solve this doubt, I guest I have to save the record in the
applicant
model from the applicanst_controller, right?

Thanks in advance

On Fri, Dec 14, 2012 at 5:48 PM, Jean [email protected] wrote:

I’m new in rails and I’ve some doubts about what kind of relationship do I
have to use. Here the case.

I have two models Offer and User, a user could apply to many offers and
offers can have many user also the users create the offers.

I think I have to use a has_many :through ralationship. for example I create
another model “Applicant”, applicant belongs_to user and belongs_to offer.
But how is the relationship from the user and offer model? for example

Read:
http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association

Thanks Jordon, I already read this article, and this is why I have my
doubt.

On Sat, Dec 15, 2012 at 7:48 AM, Jean [email protected] wrote:

User Model

has_many :offers, :dependent => :destroy

Offer Model

belongs_to :user

After solve this doubt, I guest I have to save the record in the applicant
model from the applicanst_controller, right?

since you have the applicant model as the join table between offer and
user, you won’t
need the user_id column on offer.

User Model

has_many :applicants
has_many :offers, through: applicants

Offer Model

has_many :applicants
has_many :users, through: :applicants

It doesn’t matter where you save from. But it would be more logical
to create the user first before the offer. Or if the offer already
exists,
create the user first, then the applicant.

One more thing, it may be confusing to use the model applicant. I think
you need to change to some other name like application or user_offer
which may make the associations more readable.

For more options, visit https://groups.google.com/groups/opt_out.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs