Ruby on Rails Sunday, September 23, 2018

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  nielson.rolim@gmail.com
On 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-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/b628326b-66ee-4f50-afb6-a238d2a0766f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment