On Feb 27, 2009, at 1:20 PM, Tony Tony 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 /> <%=h @shirt.primary_color_name %>
> > > <b>Secondary Color:</b> > <%=h @shirt.color2_id %><br /> <%=h @shirt.secondary_color_name %> > > > 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 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 Biedenharn http://agileconsultingllc.com r...@agileconsultingllc.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---