Modify find/where attributes

14 views
Skip to first unread message

Steve V

unread,
Sep 30, 2011, 5:23:13 PM9/30/11
to Mongoid
Is there any way to alter attribute values sent to find_or_create_by,
or where clauses all the time? I have some values that are always in a
specific format, but depending on the datasource they are coming in
from, they can be represented differenty. Some have something
prepended to them, others have differing case. Rather than try to make
sure I catch every instance for all time, I am hoping there's a way to
just do it on the model.

Model.where username: 'prefix\somename'
or
Model.find_or_create_by username: 'a.somename'

would both be modified so that they would just be {username:
'somename'}

Thanks,
Steve

Nick Hoffman

unread,
Oct 2, 2011, 1:12:49 PM10/2/11
to mon...@googlegroups.com
Hey Steve. What you asked really isn't Mongoid's job. So the answer is no, Mongoid doesn't do that. Mongoid simply sets the value of fields that you provide it with.

I suggest creating a custom class to massage the data before it's passed to #where or #find_or_create_by .

Cheers,
Nick
Reply all
Reply to author
Forward
0 new messages