In my case, I didn't need any additional attribute in my join table. So I user has_and_belongs_to_many. If you need additional attributes in you join table, you should create a model for it and use has_many through
https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
-- Nielson Rolim nielson.rolim@gmail.com
--
On Sunday, September 23, 2018 at 10:44:50 AM UTC-4, Nielson Rolim wrote:I'd suggest you create your own solution without ancestry.
I had a similar problem. I have an User model and each user must has one or more parents and one or more children. So I created a join table called users_parents with two attributes user_id and parent_id. In my User model I added to has_and_belongs_to_many entries:
has_and_belongs_to_many :parents, class_name: User, join_table: :users_parents, foreign_key: :user_id, association_foreign_key: :parent_id
has_and_belongs_to_many :children, class_name: User, join_table: :users_parents, foreign_key: :parent_id, association_foreign_key: :user_id
That way, I can add an user as parent of several users or an user as child of several users.
my_user.parents will return all my_user parents and my_user.children will return all my_user children
-- Nielson Rolim nielso...@gmail.comOn 23/09/2018 08:06, fugee ohu wrote:
I wanna create and edit people but ancestry only provides for one parent_id so how can i have two on a form--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com .
To post to this group, send email to rubyonra...@googlegroups.com .
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ .b628326b-66ee-4f50-afb6- a238d2a0766f%40googlegroups. com
For more options, visit https://groups.google.com/d/optout .
Thanks What's the difference between that and has_many through approach?
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7d11a65d-07f8-49c4-880f-7dffa0326ad9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment