I’m using custom read and write accessors in my models like :
class Order < ActiveRecord::Base
…
def date
read_attribute(:date).strftime(’%d/%m/%Y’)
end
…
end
My problem is that Form helpers like text_field, text_area, etc, don’t
seem to work with these custom accessors, is that normal ?
Thanks!
Nicolas.
The form helpers should be able to read all of the methods on the model.
My first guess was that the helper was grabbing the date attribute
instead of your date method, but that doesn’t sound possible to me since
your method should be triggered first. My second guess is that perhaps
it won’t use an accessor unless it also has a setter ( ex. a method
named date=(new_date) ). Those are just guesses though… if you tried
creating a differently named method, or adding a setter, it would be
easy enough to test out.
end
user input, AR form helpers get their display strings from
end
end
–
We develop, watch us RoR, in numbers too big to ignore.
I strongly recommend that you DO NOT CHANGE THE DEFAULT DATE FORMAT.
This will lead to unending pain and suffering, and will highly
displease the gods of MySQL.
If you change the default date format, rails will use this format to
try and write dates to the db. Mostl likely this will result in a
silent failure (at least in MySQL). Your dates will just get recorded
as ‘0000-00-00’, and you will lose fistfuls of hair trying to figure
out why.
My problem is that Form helpers like text_field, text_area, etc, don’t
seem to work with these custom accessors, is that normal ?
Thanks!
Nicolas.
Nicolas,
Yes, this is normal. And for whatever it’s worth, I’ve learned and
forgotten this at least 3 times - since I’m thinking in pure OO mode
most of the time and I expect overridden methods to get called.
Because you can do complex transformations in a custom attribute reader,
Rails gets the attribute value using _before_type_cast.
One way to think about the reason for this is because Rails has no
built-in notion of a model of the form’s state (what in the J2EE world
might be called a “form bean”). Because the default behavior of the
ActiveRecord/controller/view interaction is to read/write attributes
directly from the DB to/from the view, there’s no facility to keep track
of what’s going on the form during unsuccessful attempts to save a
particular attribute (e.g. what was typed into the form field when it’s
not valid).
In effect, you end up using a custom attribute reader/writer pair to
manage this form state. If you take the set of all such methods that
you end up writing on your ActiveRecord object, you would see that they
basically represent a “form state” or “form model” object, even though
they are methods on the model.
I’ve been thinking about this a lot. Take a look at the ActiveForm
plugin for one way to formalize this “form model” notion. In cases
where the form state can be complex, a separate form model would keep
your form state straight, but then would imply a copying step from the
form model attributes into the model attributes and the opposite.
If most of your model attributes don’t need tweaking to be displayed,
then just adding a couple of custom attribute reader/writer pairs is
probably sufficient.
But I think it’s helpful to keep the distinction between form data
concerns and model data concerns in mind.
Wes
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.