params
is special, and is set by Rails for each HTTP request. It's a hash in the form
{ :object => { :attrib1 => "value1", :attrib2 => "value2" ... }}
So you can reference the entire object with
params[:foo]
and individual attributes (fields) like
params[:foo][:bar]
A ActiveRecord model can be created in one call by passing a hash of values, as in your first example. But there are many other ways to create an instance. You can
def make_apo(some_value, another_value)
apo = Apo.new
apo.name = some_value
apo.value = another_value
end
Such a method will return an instance of Apo. In your case, if you have some values in params
, change the above to accept params
as another argument, or pass specific values when you call.
def make_apo(some_value, another_value, quantity, passed_params)
apo = Apo.new
apo.name = some_value
apo.value = another_value
apo.quantity = passed_params[:apo][:quantity]
end
But this is all a pretty unusual way of going about things. So don't just do this -- it's more by way of explaining what's going on than suggesting that you do this.