Linking twice between 2 models.

8 views
Skip to first unread message

Ben Edwards

unread,
Mar 17, 2019, 8:35:13 PM3/17/19
to rails_admin
I have 2 models and link twice between them.

walk_sections.id = walk_section_buttons.section_button_id
walk_sections.id = walk_section_buttons.next_section_button_id 

The first one gives me a drop-down/search box on the rails_admin screen to add a record to
walk_section_buttons but the second just has a field where I can type a integer. Is there any way to get the second working. I think I need to put something in the model, tried

class WalkSectionButton < ApplicationRecord
belongs_to :walk_section
belongs_to :next_walk_section_id, class_name: :walk_section
end

But is does not work.  How does rails_admin work out there is a foregn/primary key relation, what do I need to put in the model(s).

Ben

Ben Edwards

unread,
Mar 17, 2019, 8:37:06 PM3/17/19
to rails_admin
Sorry, formatting got messed up, should be:

Eduardo Moreno

unread,
Mar 18, 2019, 1:50:16 AM3/18/19
to rails...@googlegroups.com
Hi Ben, 

There is a problem with your code (next_walk_section). Take a look here in order to fix it:

https://guides.rubyonrails.org/association_basics.html#self-joins

You should change the relation with "self" to:

belongs_to :next_walk_section, foreign_key: 'next_walk_section_id', class_name: 'WalkSectionButton'

And be sure that you have a relationship (foreign_key) with the Table itself in your database.

Cheers,

Eduardo.

--
You received this message because you are subscribed to the Google Groups "rails_admin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rails_admin...@googlegroups.com.
To post to this group, send email to rails...@googlegroups.com.
Visit this group at https://groups.google.com/group/rails_admin.
For more options, visit https://groups.google.com/d/optout.

Ben Edwards

unread,
Mar 18, 2019, 6:11:03 AM3/18/19
to rails_admin
Thansk, yess there is a foregn key.  surly changing it to WalkSectionButton would make it belong to itself.  will try WalkSection.
Reply all
Reply to author
Forward
0 new messages