# Override a default attribute don't behave like expected in form\_for

**URL:** <https://www.ruby-forum.com/t/override-a-default-attribute-dont-behave-like-expected-in-form-for/196861>\
**Category:** Rails\
**Created:** [December 18, 2008, 2:23am UTC](https://www.ruby-forum.com/t/override-a-default-attribute-dont-behave-like-expected-in-form-for/196861 "2008-12-18T02:23:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [December 18, 2008, 2:23am UTC](https://www.ruby-forum.com/t/override-a-default-attribute-dont-behave-like-expected-in-form-for/196861/1 "2008-12-18T02:23:14Z")

</div>

Hello, I have a strange behavior and don’t know why.

If I override attributes from AcitveRecord object like below:

def end\_date=(date)  
date = date.to\_date unless date.blank?  
write\_attribute(:end\_date,date)  
end

def end\_date  
date = read\_attribute(:end\_date)  
I18n.localize(date) if date.present?  
end

Everything will work fine but when I try call text\_field in a form\_for  
block I get the database value and ignore end\_date getter.

\<%= f.text\_field :end\_date, :class=\>“date”, :size=\>“10” %\>

The text\_field will be filled with 2008-12-10 (mysql date) instead of  
10/12/2008 ( I18n localized)

Anyone know why we get this behavior?

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [December 18, 2008, 2:31am UTC](https://www.ruby-forum.com/t/override-a-default-attribute-dont-behave-like-expected-in-form-for/196861/2 "2008-12-18T02:31:24Z")

</div>

Looking inside rails code I found text\_field use InstanceTag and this  
call []before\_type\_cast and will return my value before turned in ruby  
object.  
What is the benefit os this behavior?

I can solve my problem forcing value on text\_field but I think it is  
ugly.

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [December 18, 2008, 2:50am UTC](https://www.ruby-forum.com/t/override-a-default-attribute-dont-behave-like-expected-in-form-for/196861/3 "2008-12-18T02:50:43Z")

</div>

I fix my problem with alias between attribute and  
attribute\_before\_type\_cast … I think active record do this because  
other kind of columns like decimals or numbers ( i’m not sure ) but  
it’s not beautyfull when we need make alias only for make text\_field  
behave like expected.
