On Nov 3, 5:45 pm, mrbless <[EMAIL PROTECTED]> wrote:
> Assuming that your yaml file is in test/fixtures directory. Do
> something like this
> #say ticket is an object going to be created
> yml=File.open("#{RAILS_ROOT}/test/fixture/get_value.yml){|ym|
> YAML::load(ym) }
> ticket.service_desk_status_id=yml["sd"]
> ["service_desk_status_id"].to_i   #assuming it to be integer
> ticket.service_desk_category_id=yml["sd"]
> ["service_desk_category_id"].to_i
> ticket.service_desk_sub_category_id=yml["sd"]
> ["service_desk_sub_category_id"].to_i
>

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

  default_values=YAML.load_file('path/to/get_value.yml')

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://
api.rubyonrails.org/classes/ActiveRecord/Callbacks.html) 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

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

--
Daniel Bush
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to