Cannot set date value

I have this controller code:
...
      field_array.each do |f|
        f = f.to_sym
        model.send("#{f}=", "#{parm_hash[f]}") if
model.attribute_present?(f)
      end
      return model
...

field_array is an array of attribute names as strings.
parm_hash is the params hash returned from the view associated with
model.

If the value associated with an attribute_key is anything but a date
then this works and the value is set. If the value is a date then it
sets nil instead.

Can anyone see what I am doing wrong?

I have this controller code:
...
field_array.each do |f|
f = f.to_sym
model.send("#{f}=", "#{parm_hash[f]}") if
model.attribute_present?(f)
end
return model
...

field_array is an array of attribute names as strings.
parm_hash is the params hash returned from the view associated with
model.

If the value associated with an attribute_key is anything but a date
then this works and the value is set. If the value is a date then it
sets nil instead.

because with a date you can't pull the value from the params hash in
one go like that - there is one value in the hash for each component
(day, month, year). There's more about this at
http://www.spacevatican.org/2008/12/3/dates-params-and-you

Fred

Frederick Cheung wrote:

because with a date you can't pull the value from the params hash in
one go like that - there is one value in the hash for each component
(day, month, year). There's more about this at
http://www.spacevatican.org/2008/12/3/dates-params-and-you

Fred

I looked at this reference, but it seems to relate specifically to the
Rails date chooser, which I am not using. I dumped the hash contents
passed to the code thus:

      print parm_hash.to_yaml
      field_array.each do |f|
        f = f.to_sym
        model.send("#{f}=", "#{parm_hash[f]}") if
model.attribute_present?(f)
      end
      return model

This is what I get:

--- !map:HashWithIndifferentAccess
client_credit_limit: "250"
superseded_after__dteblk: ""
effective_from__dteblk: 2009-03-24 09:31:16 -0400
client_credit_status: INAC
client_credit_policy: OPEN
effective_from: 2009-03-24 09:31:16 -0400
superseded_after: ""
client_credit_terms: "15"

As far as I can tell from this there are no nested hashes of any type
and the value for 'effective_from is a datetime value.

This code should resolve to:
@client.send("effective_from=",2009-03-24 09:31:16 -0400)

And should be equivalent to:
@client.effective_from=params[:client][:effective_from]

Which works without error.

Frederick Cheung wrote:

> because with a date you can't pull the value from the params hash in
> one go like that - there is one value in the hash for each component
> (day, month, year). There's more about this at
>http://www.spacevatican.org/2008/12/3/dates-params-and-you

> Fred

I looked at this reference, but it seems to relate specifically to the
Rails date chooser, which I am not using.

ah, you should have said :slight_smile:

@client.send("effective_from=",2009-03-24 09:31:16 -0400)

Are you sure this is getting called ? perhaps attribute_present? is
returning false (if the previous value of the attribute was nil)

Fred

Ahh. As you point out, attribute_present? does not refer to the
attribute itself, but to its current value. I should think that given
the PoLS this method would be better called attribute_set? but no
matter.

I suppose that I should use method_missing instead.

James Byrne wrote:

I suppose that I should use method_missing instead.

Or instead, has-attribute?

James Byrne wrote:

I suppose that I should use method_missing instead.

Or instead, has-attribute?

You could use ModelClass.column_names / ModelClass.columns_hash to see
if an appropriately named column exists.

Fred