# Creating a model that acts as a settings manager

**URL:** <https://www.ruby-forum.com/t/creating-a-model-that-acts-as-a-settings-manager/89619>\
**Category:** Rails\
**Created:** [January 5, 2006, 12:51am UTC](https://www.ruby-forum.com/t/creating-a-model-that-acts-as-a-settings-manager/89619 "2006-01-05T00:51:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/u/SSSISO-8859-1SQSIvS)\
**Post date:** [January 5, 2006, 12:51am UTC](https://www.ruby-forum.com/t/creating-a-model-that-acts-as-a-settings-manager/89619/1 "2006-01-05T00:51:25Z")

</div>

Hi,

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.

---

<div class="post-metadata">

**Author:** ![SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/u/SSSISO-8859-1SQSIvS)\
**Post date:** [January 5, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/creating-a-model-that-acts-as-a-settings-manager/89619/2 "2006-01-05T14:06:22Z")

</div>

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  
end

class MainSetting \< Setting  
end

class UserSetting \< Setting  
end

class SomeOtherSetting \< Setting  
end

# 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  
it  
would be more work than just doing it the ‘Rails Way’

Chris

---

<div class="post-metadata">

**Author:** ![SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/u/SSSISO-8859-1SQSIvS)\
**Post date:** [January 6, 2006, 2:04am UTC](https://www.ruby-forum.com/t/creating-a-model-that-acts-as-a-settings-manager/89619/3 "2006-01-06T02:04:08Z")

</div>

Thanks for your insight, I think I’ll go with it.

Ivan V.
