On Fri, Feb 27, 2009 at 1:20 PM, Tony Tony <rails-mailing-l...@andreas-s.net
> wrote:

>
> 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:
>
>  <b>Primary Color:</b>
>  <%=h @shirt.color_id %><br />
>
>  <b>Secondary Color:</b>
>  <%=h @shirt.color2_id %><br />


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

<b>Primary Color:</b>
<%=h @shirt.primary_color.name %><br />

<b>Secondary Color:</b>
<%=h @shirt.secondary_color.name if @shirt.secondary_color %><br />

Regards,
Craig

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to