Stupid Question - Need to get a field from table


#1

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


#2

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


#3

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


#4

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
removed_email_address@domain.invalid


#5

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


#6

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

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


#7

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