I’m trying to write a setter method in one of my models. Let me
explain the purpose quickly:
I have two objects in my model (that are stored in the database) that
I would like to be set as zero if a condition is true on model save.
I’d rather put this in the model and not as a database default because
I’d prefer there not to be a zero in the textfield by default for the
object in the form.
I have added the following to my model
attr_accessor :wrist_inches, :forearm_inches
and I’ve been trying this logic that hasn’t gotten me anywhere (at
least from the console). I can’t seem to actually write to the method
it just remains nil.
def wrist_inches=(wrist_inches)
@wrist_inches = “0” if male?
end
male? is a custom method for the model that returns true if the users
sex is marked as male.
If the above is not true it should just pass the numer that the user
has passed to the model.
I’ll be the first to admit that my grasp of the attr meta programming
is lacking, but I’m hoping someone can help me with my logic here not
to mention, if anyone has a way of exampling the concept of getter/
setter methods I’d be happy to listen.
Thanks,
Tim K.