Hi 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. sd: 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 Sijo -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Nov 3, 10:49 am, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi > 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. > > sd: > 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 > Sijo > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 3, 5:45 pm, mrbless <mrbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi 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? Sijo -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 3, 7:41 am, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi > 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) Fred> Sijo > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 3, 6:41 pm, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi > 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? >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/ get_value.yml'') 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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} end 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 On Nov 3, 4:21 am, Daniel Bush <dlb.id...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 3, 6:41 pm, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > Hi > > 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? > > 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/ > get_value.yml'') > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Why I am not using a migration with default values is my requirement is not that.. Sijo -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---