Bob Boyken
2006-Mar-16 20:56 UTC
[Rails] How to have multiple fields appear in collection_select?
I have a table of employees with the fields "last_name" and "first_name". I would like to populate a collection select so that what the user sees in the drop-down is "Last Name, First Name". So far, the only way I have figured out to do this is by using "find_by_sql" in my model, like so: @employees.find_by_sql("SELECT id, concat(last_name, '', '', first_name) AS full_name FROM employees ORDER BY full_name") It works fine, but someone on the irc said they thought this was the "dirty" way to go. Any suggestions on a better way to do this? -- Posted via http://www.ruby-forum.com/.
Craig White
2006-Mar-17 01:55 UTC
[Rails] How to have multiple fields appear in collection_select?
On Thu, 2006-03-16 at 21:56 +0100, Bob Boyken wrote:> I have a table of employees with the fields "last_name" and > "first_name". I would like to populate a collection select so that what > the user sees in the drop-down is "Last Name, First Name". So far, the > only way I have figured out to do this is by using "find_by_sql" in my > model, like so: > > @employees.find_by_sql("SELECT id, concat(last_name, '', '', first_name) > AS full_name FROM employees ORDER BY full_name") > > It works fine, but someone on the irc said they thought this was the > "dirty" way to go. Any suggestions on a better way to do this?---- AWDWR covers this very nicely - it''s called Aggregations. There is probably a wiki on wiki.rubyonrails.org that explains it too...but perhaps not as well as Dave does. Craig
Bob Boyken
2006-Mar-17 02:25 UTC
[Rails] Re: How to have multiple fields appear in collection_select?
Craig White wrote:> AWDWR covers this very nicely - it''s called Aggregations.Right on. Thanks! AWDWR Page 247. I appreciate your pointing me to the book instead of simply saying "do this." I''ll learn more this way. Part of the "problem" with Rails is all the unfamiliar terminology. That''s not a criticism. It''s just difficult sometimes to know the right words to use when searching for answers. Fortunately, there seems to be kind, knowlegable, and growing community. Bob -- Posted via http://www.ruby-forum.com/.
Craig White
2006-Mar-17 02:45 UTC
[Rails] Re: How to have multiple fields appear in collection_select?
On Fri, 2006-03-17 at 03:25 +0100, Bob Boyken wrote:> Craig White wrote: > > > AWDWR covers this very nicely - it''s called Aggregations. > > Right on. Thanks! AWDWR Page 247. I appreciate your pointing me to the > book instead of simply saying "do this." I''ll learn more this way.---- probably - but I may not have done you any favors as the implementation of aggregations can be difficult in certain areas and when I asked about it''s usage 6 weeks ago when I started...Bob Silva suggested that I just put the the aggregate name into the table anyway and there have been many times, I wished I did that. For example, ''validations'' on an aggregation are not simple...searching on aggregations is never as simple. Beware of what you wish for ;-) For the record, I''m still hanging in there with them but they have cost me some large amounts of time to implement methodologies where a column would have been very easy. ----> > Part of the "problem" with Rails is all the unfamiliar terminology. > That''s not a criticism. It''s just difficult sometimes to know the right > words to use when searching for answers. Fortunately, there seems to be > kind, knowlegable, and growing community.---- I believe I expressed something similar just two days ago (terminology) and I still am not always certain about brackets, curly brackets and parens...which to use when/why. Craig
oom tuinstoel
2006-Mar-17 07:38 UTC
[Rails] Re: How to have multiple fields appear in collection_select?
Or you could make a method in your model that returns "#{firstname} #{lastname}" -- Posted via http://www.ruby-forum.com/.
Bob Boyken
2006-Mar-17 15:30 UTC
[Rails] Re: How to have multiple fields appear in collection_select?
oom tuinstoel wrote:> Or you could make a method in your model that returns "#{firstname} > #{lastname}"I saw reference to that in another post, but I''m clueless as to how to implement it and make it work with collection_select. Bob -- Posted via http://www.ruby-forum.com/.
Oom Tuinstoel
2006-Mar-17 17:53 UTC
[Rails] Re: How to have multiple fields appear in collection_select?
Bob Boyken wrote:> oom tuinstoel wrote: >> Or you could make a method in your model that returns "#{firstname} >> #{lastname}" > > I saw reference to that in another post, but I''m clueless as to how to > implement it and make it work with collection_select. > > BobBob, you probably saw my post from a few days ago. I too was clueless then, but hey, here I am telling you what to do. Isn''t Rails a miracle? Here goes! In your model create a method that returns the concat of prefix and lastname: class Parent < ActiveRecord::Base has_many :children def fullname "#{prefix} #{lastname}" end end In the view for the child (probably _form.rhtml) use this: <div class="row"> <label for="child_parent_id">Parent</label> <%= collection_select(:child, :parent_id, @parents, :id, :fullname) %> </div> -- Posted via http://www.ruby-forum.com/.