# Cannot set date value

**URL:** <https://www.ruby-forum.com/t/cannot-set-date-value/163684>\
**Category:** Rails\
**Created:** [March 23, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/cannot-set-date-value/163684 "2009-03-23T22:31:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 23, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/cannot-set-date-value/163684/1 "2009-03-23T22:31:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 23, 2009, 11:12pm UTC](https://www.ruby-forum.com/t/cannot-set-date-value/163684/2 "2009-03-23T23:12:59Z")

</div>

On Mar 23, 9:31 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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

> **[Dates, params and you - Space Vatican](https://www.spacevatican.org/2008/12/3/dates-params-and-you/)**
>
> A not particularly nice area of Rails are the date and time helpers. 3 popups just isn’t a very nice bit of user interface. It’s a lot of …

Fred

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2009, 2:45pm UTC](https://www.ruby-forum.com/t/cannot-set-date-value/163684/3 "2009-03-24T14:45:26Z")

</div>

Frederick C. 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  
> [Dates, params and you - Space Vatican](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.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2009, 3:42pm UTC](https://www.ruby-forum.com/t/cannot-set-date-value/163684/4 "2009-03-24T15:42:31Z")

</div>

On Mar 24, 1:45 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Rails date chooser, which I am not using.  
> ah, you should have said 🙂

> @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

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/cannot-set-date-value/163684/5 "2009-03-24T16:06:44Z")

</div>

Well, I discovered the proximate cause why effective\_date is not set,  
but I do not know why this is happening.

```
  field_array.each do |f|
    #puts parm_hash[f].class
    f = f.to_sym
    puts "have #{f}" if model.attribute_present?(f)
    puts "missing #{f}" unless model.attribute_present?(f)
    model.send("#{f}=", "#{parm_hash[f]}") if 

```

model.attribute\_present?(f)  
end

gives:

missing effective\_from  
have client\_credit\_limit  
have client\_credit\_policy  
have client\_credit\_status  
have client\_credit\_terms  
missing superseded\_after

So, why would the model report the attribute as missing? The view for  
this looks like this:

```
  <%= f.text_field :effective_from,
                    :size => 12,
                    :class => 'datebalks'
  -%>

  <%= f.text_field :superseded_after,
                    :size => 12,
                    :class => 'datebalks'
  -%>

```

annotate gives this:

# == Schema Information

# Schema version: 20090312200034

# 

# Table name: clients

# 

# id :integer not null, primary key

# entity\_id :integer not null

# client\_credit\_limit :integer default(0), not null

# client\_credit\_policy :string(4) default(“CASH”), not null

# client\_credit\_status :string(4) default(“HOLD”), not null

# client\_credit\_terms :integer default(0), not null

# effective\_from :datetime

# superseded\_after :datetime

# changed\_at :datetime

# changed\_by :string(255)

# created\_at :datetime not null

# created\_by :string(255)

# lock\_version :integer default(0), not null

# 

and the attribute is self-evidently present because this code:

```
  field_array.each do |f|
    #puts parm_hash[f].class
    f = f.to_sym
    puts "have #{f}" if model.attribute_present?(f)
    puts "missing #{f}" unless model.attribute_present?(f)
    model.send("#{f}=", "#{parm_hash[f]}") if 

```

model.attribute\_present?(f)  
end  
puts model.id  
puts model.effective\_from  
puts model.does\_not\_exist  
return model

Fails here:  
undefined method `does\_not\_exist’ for #Client:0x2b917dbb34a8

and not on:  
puts model.effective\_from

Any ides as to what is going on?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2009, 4:31pm UTC](https://www.ruby-forum.com/t/cannot-set-date-value/163684/6 "2009-03-24T16:31:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2009, 4:33pm UTC](https://www.ruby-forum.com/t/cannot-set-date-value/163684/7 "2009-03-24T16:33:40Z")

</div>

James B. wrote:

> I suppose that I should use method\_missing instead.

Or instead, has-attribute?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2009, 10:39pm UTC](https://www.ruby-forum.com/t/cannot-set-date-value/163684/8 "2009-03-24T22:39:32Z")

</div>

On 24 Mar 2009, at 15:33, James B. wrote:

> James B. 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

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 25, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/cannot-set-date-value/163684/9 "2009-03-25T21:33:46Z")

</div>

Frederick C. wrote:

> You could use ModelClass.column\_names / ModelClass.columns\_hash to see  
> if an appropriately named column exists.

As far as I can tell from the code, that is pretty much what  
has\_attribute? does. However, upon reflection I realized that my  
approach did not accommodate virtual attributes. So, I ended up with  
this:

def attr\_set\_by\_names(attr\_names=[],param\_hash={})  
an = attr\_names.to\_ary  
return self unless param\_hash.kind\_of?(Hash) && an.length \> 0  
an.each do |f|  
self.send("#{f}=", “#{param\_hash[f]}”) if  
self.method\_exists?("#{f}=")  
end  
return self  
end

Which handles every setter method available to the class.

You assistance, as always, is greatly valued.
