Forum: Ruby on Rails Easy way to add properties to a model?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A79568f589e81bcd45837f2be2eb4260?d=identicon&s=25 Frank Spychalski (Guest)
on 2006-01-26 13:46
(Received via mailing list)
Hi,

I have a model (let's say User) and I would like to add any number of
user-defined-settings to this Model. I created a UserSetting Model
with user_id, key and value. I connected them with has_many. I know
how to access these properties from a user but I would like some
methods like user.hasProperty? / user.getProperty ...

I think I know how to write these methods/finders myself but it seems
like a pretty common problem to me, which means there could be an
easier way of doing the same. Is there?

bye
Frank
D8cb8c8cd40ddf0cd05241443a591868?d=identicon&s=25 Duane Johnson (Guest)
on 2006-01-26 18:58
(Received via mailing list)
On Jan 26, 2006, at 5:44 AM, Frank Spychalski wrote:

> easier way of doing the same. Is there?
>

Nothing automatic, but the code seems trivial anyway:

def get_property(property)
   user_settings.find_by_key(property)
end
alias has_property? get_property


Duane Johnson
(canadaduane)
http://blog.inquirylabs.com/
A79568f589e81bcd45837f2be2eb4260?d=identicon&s=25 Frank Spychalski (Guest)
on 2006-01-30 11:54
(Received via mailing list)
On 1/26/06, Duane Johnson <duane.johnson@gmail.com> wrote:
> >
> alias has_property? get_property
I think you misunderstood me. If I'm not mistaken, you code would
return the same property for every user... But these property will be
different for every user.

bye
Frank
--
http://amazing-development.com
This topic is locked and can not be replied to.