Reading value from .yml file in controller

   I have a controller open_service_desk and create action .Inside the
action when create a ticket I need to get default values of some fields
from a yml file say get_value.yml In that file I am storing values
like ..(I dont know whether this the right way)..Why I am storing this
is these are mandatory fields and I have set validation also these are
not get from the user directly.

  service_desk_status_id: 1
  service_desk_category_id: 1
  service_desk_sub_category_id: 1

            How can I read this in the action?

Thanks in advance

Assuming that your yaml file is in test/fixtures directory. Do
something like this
#say ticket is an object going to be created"#{RAILS_ROOT}/test/fixture/get_value.yml){|ym|
YAML::load(ym) }
["service_desk_status_id"].to_i #assuming it to be integer

I was going to take a different tack. Assuming this isn't a test
but just a file that is being used from somewhere to set default
for new objects, then you could do:


There are probably other ways to set defaults rather than loading up a
file like this though.

You could embed them in a class method in the model and just pull them
out when you
need them. Or maybe apply them in a callback (http:// like
before_create. Or use :default in your migration when defining the
table - might not be appropriate doing this with id's though.

Callback example:
class Foo < ActiveRecord::Base
  before_create :apply_defaults

  def apply_defaults
    self.field1='val1' if self.field1.blank?

Thanks for the reply.May I ask one more question? As Daniel Bush said
this isn't a test fixture but just a file that is being used to set
default values..So my question normally in which directory I can store
this yml file whether inside models or view or anywhere?

You can store it anywhere you want really (although I'd just stick
with letting the database handle default values)


I can't see the reason for using yaml like this - but I guess you have
your reasons.

Since you're set on having your controller load up a yaml file every
time it's accessed then I'd probably stick it in db/ somewhere. As
Fred says, you could put it anywhere.

You could lazy load it to save hitting the file system every time:
  class YourModel < ActiveRecord::Base
    def YourModel.default_values
      @@default_values ||= YAML.load_file(RAILS_ROOT+'/db/

You could create a constant in your ServiceDesk or OpenServiceDesk
model (not sure how you've set your models up)...

class ServiceDesk < ActiveRecord::Base

  DEFAULTS = {:service_desk_status_id => 1, :service_desk_category_id
=> 1, :service_desk_sub_category_id => 1}


Then in your controller you could use
yourobject.service_desk_status_id = DEFAULTS[:service_desk_status_id]

Perhaps there's a good argument against using a constant like this, I
don't know, but to me it looks like a pretty simple way to do it.

Or, as Fred suggested, create a migration to make the DB handle the
default values, like so:

change_column :tablename, :service_desk_status_id, :integer, :default
=> 1