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 /> 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 -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig Demyanovich
2009-Feb-27 19:13 UTC
Re: Stupid Question - Need to get a field from table...
On Fri, Feb 27, 2009 at 1:20 PM, Tony Tony <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2009-Feb-27 19:15 UTC
Re: Stupid Question - Need to get a field from table...
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_colorshas_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 Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig Demyanovich
2009-Feb-27 19:45 UTC
Re: Stupid Question - Need to get a field from table...
On Fri, Feb 27, 2009 at 2:33 PM, Tony Tony <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig Demyanovich 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, > CraigGood 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 -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---