# Stupid Question - Need to get a field from table

**URL:** <https://www.ruby-forum.com/t/stupid-question-need-to-get-a-field-from-table/161604>\
**Category:** Rails\
**Created:** [February 27, 2009, 7:20pm UTC](https://www.ruby-forum.com/t/stupid-question-need-to-get-a-field-from-table/161604 "2009-02-27T19:20:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 27, 2009, 7:20pm UTC](https://www.ruby-forum.com/t/stupid-question-need-to-get-a-field-from-table/161604/1 "2009-02-27T19:20:21Z")

</div>

Hi all,

I’m ashamed to ask this but I can’t figure it out. Lately my brain gets  
more burned out by the minute. Basically I’m trying to have a shirt have  
up to 2 colors. I currently have this working. When I edit a shirt, I  
can select the primary and secondary color of a shirt. But when I want  
to see the colors in the show page I can’t manage to see the name only  
the id.

For example:

**Primary Color:**  
\<%=h @shirt.color\_id %\>

**Secondary Color:**  
\<%=h @shirt.color2\_id %\>

Only displays the id (obviously) but what I want to see is the NAME OF  
THE COLOR. I just can’t think of a way to do this for some reason. Maybe  
there is a better way to do this and you guys can shed a light?

Below is the database structure and model relationship code…

## APPROVED\_COLORS (table name/structure in db)

:id (int)  
:name (string)

## SHIRTS (table name/structure in db)

:color\_id (int)  
:color2\_id (int)

## APPROVED\_COLOR.RB

belongs\_to :shirt

## SHIRT.RB

has\_many :approved\_colors

Any help on this is greatly appreciated.

Thanks,  
Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 27, 2009, 8:14pm UTC](https://www.ruby-forum.com/t/stupid-question-need-to-get-a-field-from-table/161604/2 "2009-02-27T20:14:45Z")

</div>

On Fri, Feb 27, 2009 at 1:20 PM, Tony T.  
\<removed\_email\_address@domain.invalid

> wrote:

> For example:
> 
> **Primary Color:**  
> \<%=h @shirt.color\_id %\>
> 
> **Secondary Color:**  
> \<%=h @shirt.color2\_id %\>

I’d make the following changes so that I could use more meaningful names  
for  
the colors:

# Models

class Shirt \< ActiveRecord::Base  
belongs\_to :primary\_color, :class\_name =\> “ApprovedColor”,  
:foreign\_key =\>  
“color\_id”  
belongs\_to :secondary\_color, :class\_name =\> “ApprovedColor”,  
:foreign\_key  
=\> “color2\_id”  
end

class ApprovedColor \< ActiveRecord::Base  
has\_many :shirts  
end

Even though I’ve used Rails for quite awhile now, I still have to stop  
and  
think sometimes about where the belongs\_to and the has\_many go. The  
belongs\_to goes where the foreign key exists, on Shirt in this case.

# View

**Primary Color:**  
\<%=h @shirt.primary\_color.name %\>

**Secondary Color:**  
\<%=h @shirt.secondary\_color.name if @shirt.secondary\_color %\>

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 27, 2009, 8:14pm UTC](https://www.ruby-forum.com/t/stupid-question-need-to-get-a-field-from-table/161604/3 "2009-02-27T20:14:49Z")

</div>

Ideally something like this would be great:

\<%=h @shirt.approved\_colors.color\_id.name %\>

Any ideas? I don’t know why my brain is shot today. Hate it.

-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 27, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/stupid-question-need-to-get-a-field-from-table/161604/4 "2009-02-27T20:16:31Z")

</div>

On Feb 27, 2009, at 1:20 PM, Tony T. wrote:

> For example:
> 
> **Primary Color:**  
> \<%=h @shirt.color\_id %\>  
>   
> \<%=h @shirt.primary\_color\_name %\>

> **Secondary Color:**  
> \<%=h @shirt.color2\_id %\>  
>   
> \<%=h @shirt.secondary\_color\_name %\>

> ## APPROVED\_COLOR.RB
> 
> belongs\_to :shirt
> 
> ## SHIRT.RB
> 
> has\_many :approved\_colors  
> has\_one :primary\_color, :class\_name =\> ‘ApprovedColor’, :foreign\_key  
> =\> :color\_id  
> has\_one :secondary\_color, :class\_name =\> ‘ApprovedColor’, :foreign\_key  
> =\> :color2\_id

> Any help on this is greatly appreciated.
> 
> ## Thanks, Tony

class Shirt  
def primary\_color\_name  
self.primary\_color ? self.primary\_color.name : ‘(none)’  
end  
def secondary\_color\_name  
self.secondary\_color ? self.secondary\_color.name : ‘(none)’  
end  
end

If this doesn’t quite work as-is, please take it as a strong hint and  
an exercise in using the documentation of has\_one

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 27, 2009, 8:33pm UTC](https://www.ruby-forum.com/t/stupid-question-need-to-get-a-field-from-table/161604/5 "2009-02-27T20:33:25Z")

</div>

Thanks for the replies!

Craigs solution worked like a charm.

First time I use foreign\_key. I have a feeling that while this worked,  
the way it works (specifically having to specify foreign\_key) is  
incorrect or rather defaults rails conventions. Is this the case? If so  
what can or should I do to make this work from now on?

Thanks again,  
Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 27, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/stupid-question-need-to-get-a-field-from-table/161604/6 "2009-02-27T21:21:05Z")

</div>

Craig D. wrote:

> Using the :foreign\_key option isn’t incorrect. It’s there if you need to  
> circumvent the Rails defaults for any reason. If you didn’t want to use  
> the  
> :foreign\_key option, you could use a migration to rename the color\_id  
> and  
> color2\_id columns in your shirts table to primary\_color\_id and  
> secondary\_color\_id, respectively. Then, you could just declare your  
> associations like this:
> 
> class Shirt \< ActiveRecord::Base  
> belongs\_to :primary\_color, :class\_name =\> “ApprovedColor”  
> belongs\_to :secondary\_color, :class\_name =\> “ApprovedColor”  
> end
> 
> Regards,  
> Craig

Good lord you’re good. 🙂

I really need to stop hacking and start learning more (even though I  
have learned a lot). Many thanks again for your help. I ended up going  
the foreign\_key-less way.

-Thanks

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 27, 2009, 8:47pm UTC](https://www.ruby-forum.com/t/stupid-question-need-to-get-a-field-from-table/161604/7 "2009-02-27T20:47:08Z")

</div>

On Fri, Feb 27, 2009 at 2:33 PM, Tony T.  
\<removed\_email\_address@domain.invalid

> wrote:

> Thanks for the replies!
> 
> Craigs solution worked like a charm.

Cool.

> First time I use foreign\_key. I have a feeling that while this worked,  
> the way it works (specifically having to specify foreign\_key) is  
> incorrect or rather defaults rails conventions. Is this the case? If so  
> what can or should I do to make this work from now on?

Using the :foreign\_key option isn’t incorrect. It’s there if you need to  
circumvent the Rails defaults for any reason. If you didn’t want to use  
the  
:foreign\_key option, you could use a migration to rename the color\_id  
and  
color2\_id columns in your shirts table to primary\_color\_id and  
secondary\_color\_id, respectively. Then, you could just declare your  
associations like this:

class Shirt \< ActiveRecord::Base  
belongs\_to :primary\_color, :class\_name =\> “ApprovedColor”  
belongs\_to :secondary\_color, :class\_name =\> “ApprovedColor”  
end

Regards,  
Craig
