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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On Fri, Dec 14, 2012 at 5:48 PM, Jean <josorioe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 exampleRead: http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Thanks Jordon, I already read this article, and this is why I have my doubt. On Friday, December 14, 2012 7:51:09 PM UTC-4:30, Jordon Bedwell wrote:> > On Fri, Dec 14, 2012 at 5:48 PM, Jean <joso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <javascript:>> > 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 >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/qz1rVEc8kTwJ. For more options, visit https://groups.google.com/groups/opt_out.
On Sat, Dec 15, 2012 at 7:48 AM, Jean <josorioe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > 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? >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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.