I have the following Rails model:
class CreateFoo < ActiveRecord::Migration
def self.up
create_table :foo do |t|
t.string :a
t.string :b
I found a solution that works fairly well, and is ultimately a combination of the above options. It allows for invalid params to be passed (and ignored), while valid ones are mapped correctly to the object.
def self.initialize(params={})
User.new(params.reject { |k| !User.attribute_method?(k) })
end
Now rather than calling User.new()
, call User.initialize()
. This will "filter" the correct params fairly elegantly.