Is there something in ActiveRecord to automatically convert fields to upper case? If not, is there an idiomatic place to put such conversions manually?
Jonathan Apostoles
2005-Jul-12 01:50 UTC
Is there an easier way to do this? (ActiveRecord / has_many from a form post)
I have a "has_many" association that populates a form from a few different tables that is saved in an Inspection object. Here''s the create action in the controller to populate inspection.inspection_results: def create @inspection = Inspection.new( @params[:inspection] ) @inspection.date = Time.now @inspection_results = @params[:inspection_results]; @inspection_results.each_value do |curResult| newResult = InspectionResult.new newResult.inspection_point_id = curResult["inspection_point_id"] newResult.inspection_flag_id = curResult["inspection_flag_id"] @inspection.inspection_results << newResult end @inspection.save end So I have values posted to the controller as an InspectionResult object. My question is, is there a way (through correct naming of form elements, or other method) to not have to manually do this? I tried naming the elements with names like "inspection[inspection_results[0][inspection_point_id]]" but that threw errors. I''m sure there''s a way to have this "has_many" association in an ActiveRecord get populated from a form post the same way the rest of the object is auto-populated. Any ideas? Thanks, Jonathan Apostoles
On Tue, 2005-07-12 at 01:03 +0000, Steve Downey wrote:> Is there something in ActiveRecord to automatically convert fields to upper case? > > If not, is there an idiomatic place to put such conversions manually? >I don''t believe there is a config option. The function that works best in most cases would be to override the quote_column_name function of your adapter to the following def quote_column_name(name) %Q("#{name.upcase}") end This is not fool proof. Anyplace that raw sql is passed around you will have issues because the quote_column_name function won''t be called on the function. Also, you might want to do an alias of the old method then call the old method then upcase that result in case the adapter has some rules other then simply adding quotes. John W Higgins wishdev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org