On Tuesday 14 July 2009 13:14:00 cult hero wrote:
> Okay, I thought this would be relatively straightforward but I managed
> to surprise myself with a "SystemStackError: stack level too deep"
> error.
>
> What is the bast practice for "filtering" data in a setter method of a
> model? I presumed simply overriding the setter method would be best,
> but I'm not even sure how to do that.
>
> For instance, I want to make sure usernames are always forced down to
> lowercase in a particular model. So I tried this:
>
> def username=(username)
> username.downcase!
> self.set :username => username
> end
>
> Without looking at the docs I had presumed "set" worked independently
> of the getter/setter methods. Although I'm not positive now, that
> seems to be the case (which created an infinite loop of sorts).
>
> This might be my ignorance of Ruby at work, but it seems that creating
> an alias is the only way to do this.
>
> alias :old_username= :username=
>
> def username=(username)
> username.downcase!
> self.old_username = username
> end
>
> This just doesn't seem right at all.
>
> So:
>
> 1. What's the "proper" way to override an existing setter method in
> Sequel?
>
> 2. Should I be filtering at the setter level or should I be using a
> hook or something else to accomplish this similar to validations?
>
How I usually implement this:
def username=(u)
u.downcase!
super(u)
end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---