has_many :through

11 views
Skip to first unread message

Jean

unread,
Dec 14, 2012, 6:48:01 PM12/14/12
to rubyonra...@googlegroups.com

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

Jordon Bedwell

unread,
Dec 14, 2012, 7:21:09 PM12/14/12
to rubyonra...@googlegroups.com
On Fri, Dec 14, 2012 at 5:48 PM, Jean <joso...@gmail.com> 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

Jean

unread,
Dec 14, 2012, 8:04:50 PM12/14/12
to rubyonra...@googlegroups.com
Thanks Jordon, I already read this article, and this is why I have my doubt.

Jim Ruther Nill

unread,
Dec 14, 2012, 10:07:58 PM12/14/12
to rubyonra...@googlegroups.com
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.

 

Thanks in advance

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/PlDKDwbd0rEJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
-------------------------------------------------------------
visit my blog at http://jimlabs.heroku.com
Reply all
Reply to author
Forward
0 new messages