views:

274

answers:

3

Hi,

I have just started Rails and have a basic question.

I need to add customer properties(like email id etc) so that the Rails app can read them at runtime. How can I do this ?

Can I add them to development.rb and if so how can I read it ?

In java I would have created a properties file and read it from my app.

thank you,

firemonkey

A: 

I'm not sure exactly what you are asking. I'm guessing you want an initial set of data in the database that you can access when you actually run the app? If that is so check out this other SO question How (and whether) to populate rails application with initial data

AdamB
A: 

It's a little unclear exactly what you're trying to do, but it sounds like maybe you have a model called Customer and you would like to add some attributes to it, such as email address, id, and so on?

Basically, with Active Record you don't need to do anything special to add a simple attribute (like a string or an integer). Just add a field called "email_address" to your customers table in the database, and all of your Customer objects will automagically get "email_address" and "email_address=" methods (not to mention the Customer class itself getting "find_by_email_address" and other useful methods as well). If you are adding a field containing another model, it's a bit more complicated - add a "something_id" field to the table, and an association to the class definition (eg, "has_one :something"). For more information, see the ActiveRecord api documentation.

You don't have to use any particular means to add the field to your database, but you might want to consider Migrations. Migrations are a convenient way to keep your schema versioned and synchronized across multiple machines.

If you are building your model right now, there's a short cut built in to the generator to add fields. Instead of just saying...

script/generate scaffold customer

...you can say...

script/generate scaffold customer email:string name:string badge_number:integer

...and it will generate all the appropriate fields in your migration, as well as adding them to your generated views.

John Hyland
A: 

Are you trying to do store and load configuration settings?

It's easy to store configuration settings in a yaml file and load them with initializers - loads better than littering your environment files.

This Railscast: http://railscasts.com/episodes/85-yaml-configuration-file shows you how.

mylescarrick
thank you for the answer. It solved my problem.
firemonkey