Forum: Ruby on Rails Creating a model that acts as a settings manager

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 =?ISO-8859-1?Q?Iv=E1n_Vega_Rivera?= (Guest)
on 2006-01-05 00:51
(Received via mailing list)

I was wondering how could you create a model that is tied to all the
records in one table, and retrieves the values of all records on first
load, and also handles the updating of its values.

For example, suppose I have this table:

id INT(11) PK
group CHAR(40)
name CHAR(40)
value CHAR(128)
updated_at DATETIME

Sample rows:
group   name   value
main   something   123
users   expire   45

Then, I'd like very much to do something like this in Rails:

Settings.main.something = 34
a = Settings.users.expire

And also being able to override default behaviors, because maybe I'd
want to do something special each time a specific setting is changed.

Currently, I'm just defining a method for each setting, but I keep
thinking there must be a better way.

Thanks in advance!

- Ivan V.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-01-05 14:06
(Received via mailing list)
i might suggest using the magic 'type' column instead of group

id int PK
name varchar(40)
type varchar(40) #magic column
value varchar(40)

class Setting < AR::Base

class  MainSetting < Setting

class UserSetting < Setting

class SomeOtherSetting < Setting

# and so on for all your setting types

a = MainSetting.create(:name => "something", "value" => "34")
b = UserSetting.find_by_name("expire") # a.value = 45

I'm not saying what you want to do can or cannot be done, I just think
would be more work than just doing it the 'Rails Way'

C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 Iván Vega Rivera (Guest)
on 2006-01-06 02:04
(Received via mailing list)
Thanks for your insight, I think I'll go with it.

Ivan V.
This topic is locked and can not be replied to.