I''m trying to build a selection list which I have done in various ways but this one is new to me. I have a ''facilities'' table which has all the outpatient facilities but I need to add ''Float'' and ''Main Office'' which I don''t want to add to the ''facilities'' table itself. so I figure I can add these to an array created by... def fac_list @fac_list = Facility.find(:all) + [''Float'', ''Main Office''] end and I get an array of the facilities that I need but I can''t figure out how to get this into a sorted list so I can pick this value in my view code... <%= options = [[''Select a Facility'', '''']] + @fac_list.sort { |a,b| a.value <=> b.value }.collect { |fac| [fac.value] } select ''personnel'', ''facility'', options %> but this only gets me an error of an undefined method ''value'' How do I deal with this? Craig
Hi Craig, It looks like your variable @fac_list is going to end up looking something like: [ facility1, facility2, facility3, ''Float'', ''Main Office ] So that when you say fac_list.sort { |a,b| a.value <=> b.value } This is going to translate into something like fac_list.sort { |a,b| facility1.value <=> ''Float''.value } As far as I can tell, strings don''t have a "value" method, and it''s unlikely that your facility objects do either, which is probably why you''re getting the error. Most likely, your facility object has a "name" or "title" field, so that if you want to do a meaningful comparison you''d have to use something like. fac_list.sort { |a,b| a.name <=> b.name } Which also means that your "Float" and "Main Office" strings will have to respond to the "name" message. Because of this, it might make sense to add two facility object class variables to your model corresponding to "Float" and "Main Office", especially if you''re going to be using those objects elsewhere. It might also make sense to put your sorting mechanism in the model as well. I hope this helps! Daniel -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Craig White Sent: Tuesday, May 16, 2006 2:49 PM To: rails@lists.rubyonrails.org Subject: [Rails] select list I''m trying to build a selection list which I have done in various ways but this one is new to me. I have a ''facilities'' table which has all the outpatient facilities but I need to add ''Float'' and ''Main Office'' which I don''t want to add to the ''facilities'' table itself. so I figure I can add these to an array created by... def fac_list @fac_list = Facility.find(:all) + [''Float'', ''Main Office''] end and I get an array of the facilities that I need but I can''t figure out how to get this into a sorted list so I can pick this value in my view code... <%= options = [[''Select a Facility'', '''']] + @fac_list.sort { |a,b| a.value <=> b.value }.collect { |fac| [fac.value] } select ''personnel'', ''facility'', options %> but this only gets me an error of an undefined method ''value'' How do I deal with this? Craig _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
>> @facility = Facility.find(:all, :select => ''name'')=> [#<Facility:0xb7a8fb28 @attributes={"name"=>"Summerhill"}>, ...>> @facility = @facility + [:name => "Main Office"]=> [#<Facility:0xb7a8fb28 @attributes={"name"=>"Summerhill"}>, ..., {:name=>"Main Office"}] but this of course doesn''t have the ''Facility'' class, so I need to add it to the Facility class and I can''t figure out how that is done. Craig On Tue, 2006-05-16 at 19:31 -1000, Daniel Higginbotham wrote:> Hi Craig, > > It looks like your variable @fac_list is going to end up looking something > like: > > [ facility1, > facility2, > facility3, > ''Float'', > ''Main Office ] > > So that when you say > > fac_list.sort { |a,b| a.value <=> b.value } > > This is going to translate into something like > > fac_list.sort { |a,b| > facility1.value <=> ''Float''.value } > > As far as I can tell, strings don''t have a "value" method, and it''s unlikely > that your facility objects do either, which is probably why you''re getting > the error. > > Most likely, your facility object has a "name" or "title" field, so that if > you want to do a meaningful comparison you''d have to use something like. > > fac_list.sort { |a,b| a.name <=> b.name } > > Which also means that your "Float" and "Main Office" strings will have to > respond to the "name" message. Because of this, it might make sense to add > two facility object class variables to your model corresponding to "Float" > and "Main Office", especially if you''re going to be using those objects > elsewhere. It might also make sense to put your sorting mechanism in the > model as well. > > I hope this helps! > > Daniel > > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Craig White > Sent: Tuesday, May 16, 2006 2:49 PM > To: rails@lists.rubyonrails.org > Subject: [Rails] select list > > I''m trying to build a selection list which I have done in various ways > but this one is new to me. > > I have a ''facilities'' table which has all the outpatient facilities but > I need to add ''Float'' and ''Main Office'' which I don''t want to add to the > ''facilities'' table itself. > > so I figure I can add these to an array created by... > > def fac_list > @fac_list = Facility.find(:all) + [''Float'', ''Main Office''] > end > > and I get an array of the facilities that I need but I can''t figure out > how to get this into a sorted list so I can pick this value in my view > code... > > <%= options = [[''Select a Facility'', '''']] + > @fac_list.sort { |a,b| a.value <=> b.value }.collect { > |fac| [fac.value] } > select ''personnel'', ''facility'', options %> > > but this only gets me an error of an undefined method ''value'' > > How do I deal with this? > > Craig > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
It should be easy to create Facility objects in the controller, using something like @main_office = Facility.new(:name => "Main Office") In the model, I think you could do something like def self.main_office @main_office = @main_office || self.new(:name => "Main Office") End I''m not sure if those "self"s need to be there, but that''s the idea Daniel -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Craig White Sent: Wednesday, May 17, 2006 6:09 AM To: rails@lists.rubyonrails.org Subject: RE: [Rails] select list>> @facility = Facility.find(:all, :select => ''name'')=> [#<Facility:0xb7a8fb28 @attributes={"name"=>"Summerhill"}>, ...>> @facility = @facility + [:name => "Main Office"]=> [#<Facility:0xb7a8fb28 @attributes={"name"=>"Summerhill"}>, ..., {:name=>"Main Office"}] but this of course doesn''t have the ''Facility'' class, so I need to add it to the Facility class and I can''t figure out how that is done. Craig On Tue, 2006-05-16 at 19:31 -1000, Daniel Higginbotham wrote:> Hi Craig, > > It looks like your variable @fac_list is going to end up looking something > like: > > [ facility1, > facility2, > facility3, > ''Float'', > ''Main Office ] > > So that when you say > > fac_list.sort { |a,b| a.value <=> b.value } > > This is going to translate into something like > > fac_list.sort { |a,b| > facility1.value <=> ''Float''.value } > > As far as I can tell, strings don''t have a "value" method, and it''sunlikely> that your facility objects do either, which is probably why you''re getting > the error. > > Most likely, your facility object has a "name" or "title" field, so thatif> you want to do a meaningful comparison you''d have to use something like. > > fac_list.sort { |a,b| a.name <=> b.name } > > Which also means that your "Float" and "Main Office" strings will have to > respond to the "name" message. Because of this, it might make sense toadd> two facility object class variables to your model corresponding to "Float" > and "Main Office", especially if you''re going to be using those objects > elsewhere. It might also make sense to put your sorting mechanism in the > model as well. > > I hope this helps! > > Daniel > > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Craig White > Sent: Tuesday, May 16, 2006 2:49 PM > To: rails@lists.rubyonrails.org > Subject: [Rails] select list > > I''m trying to build a selection list which I have done in various ways > but this one is new to me. > > I have a ''facilities'' table which has all the outpatient facilities but > I need to add ''Float'' and ''Main Office'' which I don''t want to add to the > ''facilities'' table itself. > > so I figure I can add these to an array created by... > > def fac_list > @fac_list = Facility.find(:all) + [''Float'', ''Main Office''] > end > > and I get an array of the facilities that I need but I can''t figure out > how to get this into a sorted list so I can pick this value in my view > code... > > <%= options = [[''Select a Facility'', '''']] + > @fac_list.sort { |a,b| a.value <=> b.value }.collect { > |fac| [fac.value] } > select ''personnel'', ''facility'', options %> > > but this only gets me an error of an undefined method ''value'' > > How do I deal with this? > > Craig > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Well, I''m trying to use a model other than self.>> @facility = Facility.find(:all, :select => ''name'') ||Facility.new(:name => ''Main Office'') This doesn''t work (in console), ''Main Office'' isn''t added to the array.>> @facility = Facility.find(:all, :select => ''name'')=> [#<Facility:0xb790d548 @attributes={"name"=>"Summerhill"}>, ...snip...>> @home_office = Facility.new=> #<Facility:0xb790800c @attributes={"fax_number"=>nil, "city"=>nil, "name"=>nil, "beds_a"=>nil, "zip_code_plus_4"=>nil, "pltype"=>nil, "beds_m"=>nil, "phone_number"=>nil, "beds_f"=>nil, "address"=>nil, "state"=>nil}, @new_record=true>>> @home_office.name = ''Main Office''=> "Main Office">> @home_office=> #<Facility:0xb790800c @attributes={"fax_number"=>nil, "city"=>nil, "name"=>"Main Office", "beds_a"=>nil, "zip_code_plus_4"=>nil, "pltype"=>nil, "beds_m"=>nil, "phone_number"=>nil, "beds_f"=>nil, "address"=>nil, "state"=>nil}, @new_record=true>>> @facility = @facility + @home_officeTypeError: can''t convert Facility into Array from (irb):73:in `+'' from (irb):73 This is proving to be difficult to do and I thought it would be easy... Thanks Craig On Wed, 2006-05-17 at 08:02 -1000, Daniel Higginbotham wrote:> It should be easy to create Facility objects in the controller, using > something like > > @main_office = Facility.new(:name => "Main Office") > > > In the model, I think you could do something like > > def self.main_office > @main_office = @main_office || self.new(:name => "Main Office") > End > > I''m not sure if those "self"s need to be there, but that''s the idea > > > Daniel > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Craig White > Sent: Wednesday, May 17, 2006 6:09 AM > To: rails@lists.rubyonrails.org > Subject: RE: [Rails] select list > > >> @facility = Facility.find(:all, :select => ''name'') > => [#<Facility:0xb7a8fb28 @attributes={"name"=>"Summerhill"}>, ... > > >> @facility = @facility + [:name => "Main Office"] > => [#<Facility:0xb7a8fb28 @attributes={"name"=>"Summerhill"}>, ..., > {:name=>"Main Office"}] > > but this of course doesn''t have the ''Facility'' class, so I need to add > it to the Facility class and I can''t figure out how that is done. > > Craig > > On Tue, 2006-05-16 at 19:31 -1000, Daniel Higginbotham wrote: > > Hi Craig, > > > > It looks like your variable @fac_list is going to end up looking something > > like: > > > > [ facility1, > > facility2, > > facility3, > > ''Float'', > > ''Main Office ] > > > > So that when you say > > > > fac_list.sort { |a,b| a.value <=> b.value } > > > > This is going to translate into something like > > > > fac_list.sort { |a,b| > > facility1.value <=> ''Float''.value } > > > > As far as I can tell, strings don''t have a "value" method, and it''s > unlikely > > that your facility objects do either, which is probably why you''re getting > > the error. > > > > Most likely, your facility object has a "name" or "title" field, so that > if > > you want to do a meaningful comparison you''d have to use something like. > > > > fac_list.sort { |a,b| a.name <=> b.name } > > > > Which also means that your "Float" and "Main Office" strings will have to > > respond to the "name" message. Because of this, it might make sense to > add > > two facility object class variables to your model corresponding to "Float" > > and "Main Office", especially if you''re going to be using those objects > > elsewhere. It might also make sense to put your sorting mechanism in the > > model as well. > > > > I hope this helps! > > > > Daniel > > > > > > -----Original Message----- > > From: rails-bounces@lists.rubyonrails.org > > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Craig White > > Sent: Tuesday, May 16, 2006 2:49 PM > > To: rails@lists.rubyonrails.org > > Subject: [Rails] select list > > > > I''m trying to build a selection list which I have done in various ways > > but this one is new to me. > > > > I have a ''facilities'' table which has all the outpatient facilities but > > I need to add ''Float'' and ''Main Office'' which I don''t want to add to the > > ''facilities'' table itself. > > > > so I figure I can add these to an array created by... > > > > def fac_list > > @fac_list = Facility.find(:all) + [''Float'', ''Main Office''] > > end > > > > and I get an array of the facilities that I need but I can''t figure out > > how to get this into a sorted list so I can pick this value in my view > > code... > > > > <%= options = [[''Select a Facility'', '''']] + > > @fac_list.sort { |a,b| a.value <=> b.value }.collect { > > |fac| [fac.value] } > > select ''personnel'', ''facility'', options %> > > > > but this only gets me an error of an undefined method ''value'' > > > > How do I deal with this? > > > > Craig > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
OK - thanks for the help Daniel...I had to split it into steps but I got it. Craig On Wed, 2006-05-17 at 11:43 -0700, Craig White wrote:> Well, I''m trying to use a model other than self. > > >> @facility = Facility.find(:all, :select => ''name'') || > Facility.new(:name => ''Main Office'') > > This doesn''t work (in console), ''Main Office'' isn''t added to the array. > > >> @facility = Facility.find(:all, :select => ''name'') > => [#<Facility:0xb790d548 @attributes={"name"=>"Summerhill"}>, > ...snip... > > >> @home_office = Facility.new > => #<Facility:0xb790800c @attributes={"fax_number"=>nil, "city"=>nil, > "name"=>nil, "beds_a"=>nil, "zip_code_plus_4"=>nil, "pltype"=>nil, > "beds_m"=>nil, "phone_number"=>nil, "beds_f"=>nil, "address"=>nil, > "state"=>nil}, @new_record=true> > > >> @home_office.name = ''Main Office'' > => "Main Office" > > >> @home_office > => #<Facility:0xb790800c @attributes={"fax_number"=>nil, "city"=>nil, > "name"=>"Main Office", "beds_a"=>nil, "zip_code_plus_4"=>nil, > "pltype"=>nil, "beds_m"=>nil, "phone_number"=>nil, "beds_f"=>nil, > "address"=>nil, "state"=>nil}, @new_record=true> > > >> @facility = @facility + @home_office > TypeError: can''t convert Facility into Array > from (irb):73:in `+'' > from (irb):73 > > This is proving to be difficult to do and I thought it would be easy... > > Thanks > > Craig > > On Wed, 2006-05-17 at 08:02 -1000, Daniel Higginbotham wrote: > > It should be easy to create Facility objects in the controller, using > > something like > > > > @main_office = Facility.new(:name => "Main Office") > > > > > > In the model, I think you could do something like > > > > def self.main_office > > @main_office = @main_office || self.new(:name => "Main Office") > > End > > > > I''m not sure if those "self"s need to be there, but that''s the idea > > > > > > Daniel > > > > -----Original Message----- > > From: rails-bounces@lists.rubyonrails.org > > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Craig White > > Sent: Wednesday, May 17, 2006 6:09 AM > > To: rails@lists.rubyonrails.org > > Subject: RE: [Rails] select list > > > > >> @facility = Facility.find(:all, :select => ''name'') > > => [#<Facility:0xb7a8fb28 @attributes={"name"=>"Summerhill"}>, ... > > > > >> @facility = @facility + [:name => "Main Office"] > > => [#<Facility:0xb7a8fb28 @attributes={"name"=>"Summerhill"}>, ..., > > {:name=>"Main Office"}] > > > > but this of course doesn''t have the ''Facility'' class, so I need to add > > it to the Facility class and I can''t figure out how that is done. > > > > Craig > > > > On Tue, 2006-05-16 at 19:31 -1000, Daniel Higginbotham wrote: > > > Hi Craig, > > > > > > It looks like your variable @fac_list is going to end up looking something > > > like: > > > > > > [ facility1, > > > facility2, > > > facility3, > > > ''Float'', > > > ''Main Office ] > > > > > > So that when you say > > > > > > fac_list.sort { |a,b| a.value <=> b.value } > > > > > > This is going to translate into something like > > > > > > fac_list.sort { |a,b| > > > facility1.value <=> ''Float''.value } > > > > > > As far as I can tell, strings don''t have a "value" method, and it''s > > unlikely > > > that your facility objects do either, which is probably why you''re getting > > > the error. > > > > > > Most likely, your facility object has a "name" or "title" field, so that > > if > > > you want to do a meaningful comparison you''d have to use something like. > > > > > > fac_list.sort { |a,b| a.name <=> b.name } > > > > > > Which also means that your "Float" and "Main Office" strings will have to > > > respond to the "name" message. Because of this, it might make sense to > > add > > > two facility object class variables to your model corresponding to "Float" > > > and "Main Office", especially if you''re going to be using those objects > > > elsewhere. It might also make sense to put your sorting mechanism in the > > > model as well. > > > > > > I hope this helps! > > > > > > Daniel > > > > > > > > > -----Original Message----- > > > From: rails-bounces@lists.rubyonrails.org > > > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Craig White > > > Sent: Tuesday, May 16, 2006 2:49 PM > > > To: rails@lists.rubyonrails.org > > > Subject: [Rails] select list > > > > > > I''m trying to build a selection list which I have done in various ways > > > but this one is new to me. > > > > > > I have a ''facilities'' table which has all the outpatient facilities but > > > I need to add ''Float'' and ''Main Office'' which I don''t want to add to the > > > ''facilities'' table itself. > > > > > > so I figure I can add these to an array created by... > > > > > > def fac_list > > > @fac_list = Facility.find(:all) + [''Float'', ''Main Office''] > > > end > > > > > > and I get an array of the facilities that I need but I can''t figure out > > > how to get this into a sorted list so I can pick this value in my view > > > code... > > > > > > <%= options = [[''Select a Facility'', '''']] + > > > @fac_list.sort { |a,b| a.value <=> b.value }.collect { > > > |fac| [fac.value] } > > > select ''personnel'', ''facility'', options %> > > > > > > but this only gets me an error of an undefined method ''value'' > > > > > > How do I deal with this? > > > > > > Craig > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails