# Form helpers don't work with facade columns?

**URL:** <https://www.ruby-forum.com/t/form-helpers-dont-work-with-facade-columns/65587>\
**Category:** Rails\
**Created:** [August 19, 2006, 2:04pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-work-with-facade-columns/65587 "2006-08-19T14:04:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 19, 2006, 2:04pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-work-with-facade-columns/65587/1 "2006-08-19T14:04:45Z")

</div>

Hello all,

I’ve been trying to use a facade column but I notice that the form  
helpers don’t seem to respect the facade.

In the following example I’m trying to story the prices as Integer  
values but present them to the user as decimals.

class Product \< ActiveRecord::Base  
def price=(new\_price)  
write\_attribute(:price, Float(new\_price)\*100)  
end

def price  
read\_attribute(:price)/100.00  
end  
end

and for the most part that works great, I can call product.price and  
get  
9.99, I call product.price=9.99 and it gets stored in the DB as 999.

However, when I use the form helpers in the views…

\<%= text\_field ‘product’, ‘price’ %\>

I get a text field with the value 999. I assume this is because the  
form helpers are calling for the attributes in a more direct fashion.  
Either with read\_attribute or directly accessing @attributes.

My question is: is this a bug with the form helpers (or is there a  
reason they use lower level code), have I made a mistake in my code, or  
have I just approached the problem in the wrong way? I think probably  
one of the latter two.

Many thanks for your time.

Gavin.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2006, 10:41am UTC](https://www.ruby-forum.com/t/form-helpers-dont-work-with-facade-columns/65587/2 "2006-08-22T10:41:29Z")

</div>

Thanks for that,

The trouble is, and I’m quoting from “Ruby on Rails” 2nd edition page  
391

class ProductData \< ActiveRecord::Base  
CUBITS\_TO\_INCHES = 18  
def length  
read\_attribute(“length”) \* CUBITS\_TO\_INCHES  
end

def length=(inches)  
write\_attribute(“length”, Float(inches) / CUBITS\_TO\_INCHES)  
end  
end

…the example from the book describes a situation where the field is  
overwritten with an accessor method with the same name. Maybe it’s  
more an issue for the authors than the framework but if the ability to  
use the facade cleanly isn’t carried through all the layers of MVC then  
perhaps it’s worth mentioning.

In the meantime I’ll probably adopt your practice. A quick test shows  
that non-attribute methods are respected.

Thanks for your help,  
Gavin

Alex W. wrote:

> ```
> end
> 
> ```
> 
> data it stores is not the actual password.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 21, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-work-with-facade-columns/65587/3 "2006-08-21T17:21:39Z")

</div>

removed\_email\_address@domain.invalid wrote:

> ```
> write_attribute(:price, Float(new_price)*100)
> 
> ```
> 
> one of the latter two.
> 
> Many thanks for your time.
> 
> Gavin.

I have never used form helper with a method that overwrites an attrbute  
like that, but I know that form helpers DO work with abstract methods.  
So you can give your column a name like price\_as\_int. This way the  
form helper will not get confused.

then:

class Product \< ActiveRecord::Base  
def price=(new\_price)  
self.price\_as\_int = Float(new\_price) \* 100  
end

```
def price
  price_as_int / 100.00
end

```

end

This also may be better practice since it serves as a reminder that the  
price column’s value is not valid without some conversion. Much like  
the acts\_as\_authenticated plugin uses a column called  
“crypted\_password” rather than just “password” to remind you that the  
data it stores is not the actual password.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2006, 11:49am UTC](https://www.ruby-forum.com/t/form-helpers-dont-work-with-facade-columns/65587/4 "2006-08-23T11:49:02Z")

</div>

Just as a quick follow up…

## file: form\_helper.rb from the stable svn branch line: 337

```
  def value_before_type_cast
    unless object.nil?
      object.respond_to?(@method_name + "_before_type_cast") ?
        object.send(@method_name + "_before_type_cast") :
        object.send(@method_name)
    end
  end

```

* * *

If the object responds to \_before\_type\_cast then that  
function gets called in preference to the facade. This means that, as  
Alex mentioned above, one way round the problem is to use an alterative  
name. I’m not sure why this should be the case though. Is there a  
reason that the \_before\_type\_cast should be used rather than just  
stringing the attribute?

Cheers,  
Gavin.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 26, 2006, 11:05am UTC](https://www.ruby-forum.com/t/form-helpers-dont-work-with-facade-columns/65587/5 "2006-08-26T11:05:52Z")

</div>

Gavin wrote:

> ```
> end
> 
> ```
> 
> Cheers,  
> Gavin.

Interesting…

I also want to know why this is like this. Is this the same on edge as  
well?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 20, 2006, 1:34am UTC](https://www.ruby-forum.com/t/form-helpers-dont-work-with-facade-columns/65587/6 "2006-09-20T01:34:55Z")

</div>

Did either of you guys figure out why this is the case?

I keep “re-discovering it” because I expect to be able to override my  
accessors, and therefore it is a total pain in the a\*\*.

Wes
