@demetriusolsen
2011-Jul-03 06:48 UTC
displaying attribute from related table with a collection_select
I have two tables: Area, AreaType Area belongs_to :area_type AreaType has_many :areas In my view, I''d like to select from a list of Areas, however, instead of using the :area_type_id attribute, I would like to use the associated area_type names. Here''s what it looks like now: <%= f.collection_select(:area_id, Area.where(:subject_id => @search.subject_id), :id, :area_type_id, {:include_blank => true} ) %> What I want is: <%= f.collection_select(:area_id, Area.where(:subject_id => @search.subject_id), :id, area.area_type.name, {:include_blank => true} ) %> Anyone know how I would go about creating the area.area_type.name "symbol" to user here? Thanks in advance! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
@demetriusolsen
2011-Jul-03 07:09 UTC
Re: displaying attribute from related table with a collection_select
Well that was easy! After a little trial and error, I created a new method in the area.rb and used the name of that method in the collection select :-) Cheers On Jul 2, 11:48 pm, "@demetriusolsen" <demetri...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> I have two tables: Area, AreaType > > Area belongs_to :area_type > AreaType has_many :areas > > In my view, I''d like to select from a list of Areas, however, instead > of using the :area_type_id attribute, I would like to use the > associated area_type names. Here''s what it looks like now: > > <%= f.collection_select(:area_id, Area.where(:subject_id => > @search.subject_id), :id, :area_type_id, {:include_blank => true} ) %> > > What I want is: > <%= f.collection_select(:area_id, Area.where(:subject_id => > @search.subject_id), :id, area.area_type.name, {:include_blank => > true} ) %> > > Anyone know how I would go about creating the area.area_type.name > "symbol" to user here? > > Thanks in advance!-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.