ancestry gem creating and editing people view forms

31 views
Skip to first unread message

fugee ohu

unread,
Sep 23, 2018, 7:06:58 AM9/23/18
to Ruby on Rails: Talk
I wanna create and edit people but ancestry only provides for one parent_id so how can i have two on a form

Nielson Rolim

unread,
Sep 23, 2018, 10:44:50 AM9/23/18
to rubyonra...@googlegroups.com

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.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-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.

fugee ohu

unread,
Sep 29, 2018, 10:21:47 AM9/29/18
to Ruby on Rails: Talk
Thanks What's the difference between that and has_many through approach?

Nielson Rolim

unread,
Oct 1, 2018, 1:00:31 PM10/1/18
to rubyonra...@googlegroups.com

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
nielso...@gmail.com
Reply all
Reply to author
Forward
0 new messages