I''m looking for the most efficient way to convert all of the strings in a given model to uppercase. For instance if I have @users = User.find(:all) how would I convert all of the user data to uppercase? Is there a way to do this in the model? - Jim
David Felstead
2005-Oct-25 02:47 UTC
Re: DRY Method for Converting All Strings to Uppercase.
Well, it depends when you want it done... have a look at ActiveRecord::Callbacks for when it can be done easily, but for example, you could do this (untested): class User < ActiveRecord::Base before_save :upcase_fields # Before the user is saved this is called def upcase_fields self.name.upcase! self.login.upcase! end end If you wanted to just do it on the fly in your model (less efficient, but you''re guaranteed never to get a lowercase attribute), you could override read_attribute in your class, something like (very untested!!!): def User < ActiveRecord::Base UPCASE_FIELDS = %w{login surname first_name city} def read_attribute(attr_name) attr = super(attr_name) # Upcase the field if it''s marked as such, and not nil attr && UPCASE_FIELDS.include?(attr_name) ? attr.upcase : attr end end UPCASE_FIELDS might be better as a hash, though for small number of fields it''s probably a negligible difference. Cheers! -DF On 10/25/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote:> I''m looking for the most efficient way to convert all of the strings > in a given model to uppercase. For instance if I have @users > User.find(:all) how would I convert all of the user data to > uppercase? Is there a way to do this in the model? > > - Jim > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
David Felstead wrote:> def User < ActiveRecord::Base > > UPCASE_FIELDS = %w{login surname first_name city} > > def read_attribute(attr_name) > attr = super(attr_name) > # Upcase the field if it''s marked as such, and not nil > attr && UPCASE_FIELDS.include?(attr_name) ? attr.upcase : attr > end > > endSurely if you want to catch *all* string fields, then: def read_attribute(attr_name) a = super(attr_name) if a.respond_to? ''upcase'' then a.upcase else a end end would be more appropriate? Or even better: class String def user_filter self.upcase end end class Fixnum def user_filter self - 42 end end class Hash def user_filter self.default=''CHUNKY BACON!'' self end end module ActiveRecord class Base def self.filter_name "#{self.name.downcase}_filter" end end end class User < ActiveRecord::Base def read_attribute(attr_name) a = super(attr_name) f = self.filter_name if a.respond_to? f then a.send(f) else a end end end But that way lies madness... -- Alex
Thanks guys. The Ruby language is powerful indeed :) On Oct 25, 2005, at 3:05 AM, Alex Young wrote:> David Felstead wrote: > >> def User < ActiveRecord::Base >> >> UPCASE_FIELDS = %w{login surname first_name city} >> >> def read_attribute(attr_name) >> attr = super(attr_name) >> # Upcase the field if it''s marked as such, and not nil >> attr && UPCASE_FIELDS.include?(attr_name) ? attr.upcase : attr >> end >> >> end >> > > Surely if you want to catch *all* string fields, then: > > def read_attribute(attr_name) > a = super(attr_name) > if a.respond_to? ''upcase'' then a.upcase else a end > end > > would be more appropriate? Or even better: > > > class String > def user_filter > self.upcase > end > end > > class Fixnum > def user_filter > self - 42 > end > end > > class Hash > def user_filter > self.default=''CHUNKY BACON!'' > self > end > end > > module ActiveRecord > class Base > def self.filter_name > "#{self.name.downcase}_filter" > end > end > end > > class User < ActiveRecord::Base > def read_attribute(attr_name) > a = super(attr_name) > f = self.filter_name > if a.respond_to? f then a.send(f) else a end > end > end > > But that way lies madness... > > -- > Alex > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >