Forum: Ruby on Rails want to show content of foreign key in rhtml-- Newbie

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2007-07-06 07:38
(Received via mailing list)
Hi I have a scaffold CRUD app going and have managed to get my foreign
key to populate a select dropdown list in the edit/new _form.rhtml
It seemed logical to have a foreign key with a select list.
Unfortunately, I can't figure out how to get the content of that same
foreign key to appear in show or list actions.

My tables are:

artworks

categories

In the artworks table there is a column named category_id and it
belongs_to :category, :foreign_key => 'category_name'

In the categories table there is a column named category_name

Any tips or help here? Do I need to do something in my category.rb or
artwork.rb models? or in my admin_controller.rb controller?

The default scaffold template show.rhtml uses:
for columns in Artwork.content_columns

(I'm assuming this is the same as:
Artwork.content_columns.each do |column| ... end  )

I checked out the content_columns method in the API and says that it
strips out an array item that ends with _id  but that seems to be the
logical choice for a foreign key!
So, if I change the schema to use a different name for the foreign
key, will it work? Or am I way lost. (I feel lost)

Please be gentle and verbose!
(Guest)
on 2007-07-07 00:52
(Received via mailing list)
Wow, this is a pretty unresponsive list here.
I'm glad Ruby-Talk is not like this.
tmac (Guest)
on 2007-07-07 02:02
(Received via mailing list)
On Jul 5, 8:36 pm, "removed_email_address@domain.invalid"
<removed_email_address@domain.invalid> wrote:
> categories
>
> In the artworks table there is a column named category_id and it
> belongs_to :category, :foreign_key => 'category_name'

I think category_id is your foreign key there, just delete the
foreign_key arg... you don't need it at all since AR will default to
category_id in this case... and I suspect it is causing you trouble.

>
> In the categories table there is a column named category_name
>
> Any tips or help here? Do I need to do something in my category.rb or
> artwork.rb models? or in my admin_controller.rb controller?

don't think so

>
> The default scaffold template show.rhtml uses:
> for columns in Artwork.content_columns
>
> (I'm assuming this is the same as:
> Artwork.content_columns.each do |column| ... end  )

They're actually not exactly the same, the variables are scoped
differently but in this instance they likely work the same.

>
> I checked out the content_columns method in the API and says that it
> strips out an array item that ends with _id  but that seems to be the
> logical choice for a foreign key!
> So, if I change the schema to use a different name for the foreign
> key, will it work? Or am I way lost. (I feel lost)
>
> Please be gentle and verbose!

try using column_names (Artwork.column_names) instead.

good luck!
Tim
(Guest)
on 2007-07-08 21:21
(Received via mailing list)
Thanks for the help. I found my answers by working through the
tutorial at OnLamp. It answered my questions while being simple and
focused enough to prevent other things compounding my questions. The
Agile book has a lot in it, but the tutorial there is just a little
too kitchen-sink to grasp some aspects well.

Turns out, there were indeed some automagic naming conventions I
should have used, but didn't and that threw me off.

Rails is certainly different from many Ruby programs. I'm starting to
see that Rails does more for me if I do a lot more the way it expects
things.
This topic is locked and can not be replied to.