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.
Frank S. (Guest)
on 2006-01-26 14: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
Duane J. (Guest)
on 2006-01-26 19:58
(Received via mailing list)
On Jan 26, 2006, at 5:44 AM, Frank S. 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 J.
(canadaduane)
http://blog.inquirylabs.com/
Frank S. (Guest)
on 2006-01-30 12:54
(Received via mailing list)
On 1/26/06, Duane J. <removed_email_address@domain.invalid> 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.