Forum: Ruby on Rails Stupid Question - Need to get a field from table...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tony T. (Guest)
on 2009-02-27 20:20
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
Craig D. (Guest)
on 2009-02-27 21:14
(Received via mailing list)
On Fri, Feb 27, 2009 at 1:20 PM, Tony T.
<removed_email_address@domain.invalid
> wrote:

>
> 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
Tony T. (Guest)
on 2009-02-27 21:14
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
Rob B. (Guest)
on 2009-02-27 21:16
(Received via mailing list)
On Feb 27, 2009, at 1:20 PM, Tony T. wrote:
>
>
> 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 %>

>
>
>
> 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
Tony T. (Guest)
on 2009-02-27 21:33
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
Craig D. (Guest)
on 2009-02-27 21:47
(Received via mailing list)
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
Tony T. (Guest)
on 2009-02-27 22:21
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
This topic is locked and can not be replied to.