Problem with Overriding Accessors

220 views
Skip to first unread message

andrew8088

unread,
Mar 29, 2011, 7:15:14 AM3/29/11
to DataMapper
I'm having an issue with overriding accessors; here's my model:

class User
include DataMapper::Resource

property :id, Serial
property :username, String

def username= new_username
@usename = new_username.downcase
end

end

The problem is that, when using a custom accessor like I have here,
the field doesn't get set; it gets set to null. I've found that I can
do this:

def username= new_username
super new_username.downcase
end

And that works fine. Of course, not overriding the accessor works
too.

I copied the example from the documentation (http://rubydoc.info/
github/datamapper/dm-core/master/DataMapper/Property) and had the same
problem with it.

Has the syntax changed? Am I doing something wrong?

Thanks!

PS - if it matters, I'm using dm-core 1.1.0 in Ruby 1.9.2 on Snow
Leopard

Jonathan Stott

unread,
Mar 29, 2011, 8:01:37 AM3/29/11
to datam...@googlegroups.com
Hi Andrew

The use of super is the correct usage. That invokes the accessor, and
thus the appropriate dirtiness tracking mechanisms, which in turn
ensure the changed property is persisted to the datastore. The
example in the docs is currently incorrect, though that syntax might
work at some point in the future (if/when improved dirtiness tracking
is added). For now, use super. Or "self[:property_name] =
'something'"

Regards
Jon

> --
> You received this message because you are subscribed to the Google Groups "DataMapper" group.
> To post to this group, send email to datam...@googlegroups.com.
> To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.
>
>

andrew8088

unread,
Mar 29, 2011, 8:29:43 AM3/29/11
to DataMapper
Thanks!

On Mar 29, 8:01 am, Jonathan Stott <jonathan.st...@gmail.com> wrote:
> Hi Andrew
>
> The use of super is the correct usage.  That invokes the accessor, and
> thus the appropriate dirtiness tracking mechanisms, which in turn
> ensure the changed property is persisted to the datastore.  The
> example in the docs is currently incorrect, though that syntax might
> work at some point in the future (if/when improved dirtiness tracking
> is added).  For now, use super.  Or "self[:property_name] =
> 'something'"
>
> Regards
> Jon
>
Reply all
Reply to author
Forward
0 new messages