Hi All I''m trying to create an ActiveRecord object ''directly'' from some JSON data. By ''directly'' - I mean without ''walking'' the parsed JSON data, but rather just passing the parsed JSON data to a method that will process all the data. To keep the question simple - let''s say I have two objects. A Survey and a Question. Each Survey can have many Questions. Here''s a script containing the json data and my attempt at creating an ActiveRecord object directly from the json string ... # begin test.rb script json = ''{ "name":"Fundraising Survey", "description":"A survey about a fundraiser", "questions":[ {"text":"How clearly did our organization explain our fundraising goals?"}, {"text":"Was the cost of attending our fundraiser too high, too low, or about right?"} ] }'' survey = Survey.new survey.attributes = ActiveSupport::JSON.decode(json) # end test.rb script For reference = here is the ruby code for the Survey and Question classes ... class Survey < ActiveRecord::Base has_many :questions, :dependent => :destroy accepts_nested_attributes_for :questions, :allow_destroy => true, :reject_if => lambda {|attrs| attrs[''text''].blank? } attr_accessible :name, :description end class Question < ActiveRecord::Base belongs_to :survey acts_as_list :scope => :survey validates_presence_of :text, :message => ''Text can not be blank'' attr_accessible :survey_id, :text end I get this error when I run the test.rb script (see code snippet above) ... /Users/dse/.rvm/gems/ruby-1.9.3-p327@sq/gems/activerecord-3.2.9/lib/active_record/associations/association.rb:204:in `raise_on_type_mismatch'': Question(#70357035179920) expected, got Hash(#70357017137920) (ActiveRecord::AssociationTypeMismatch) Any help would be greatly appreciated. Thanks again Dave -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ngpKVHYpMVoJ. For more options, visit https://groups.google.com/groups/opt_out.
On Wed, Dec 19, 2012 at 5:30 AM, dekhaus <dekhaus-ee4meeAH724@public.gmane.org> wrote:> Hi All > > Hi,> I''m trying to create an ActiveRecord object ''directly'' from some JSON > data. By ''directly'' - I mean without ''walking'' the parsed JSON data, but > rather just passing the parsed JSON data to a method that will process all > the data. > > To keep the question simple - let''s say I have two objects. A Survey and > a Question. Each Survey can have many Questions. > > Here''s a script containing the json data and my attempt at creating an > ActiveRecord object directly from the json string ... > > # begin test.rb script > > json = ''{ > > "name":"Fundraising Survey", > > "description":"A survey about a fundraiser", > > "questions":[ >Try with "questions_attributes" instead. See the doc for ActiveRecord::NestedAttributes> {"text":"How clearly did our organization explain our fundraising > goals?"}, > > {"text":"Was the cost of attending our fundraiser too high, too low, or > about right?"} > > ] > > }'' > > survey = Survey.new > > survey.attributes = ActiveSupport::JSON.decode(json) >survey = Survey.new(ActiveSupport::JSON.decode(json)) I have never tried with survey.attributes.> # end test.rb script > > > For reference = here is the ruby code for the Survey and Question classes > ... > > class Survey < ActiveRecord::Base > > has_many :questions, :dependent => :destroy > > accepts_nested_attributes_for :questions, :allow_destroy => true, > > :reject_if => lambda {|attrs| > attrs[''text''].blank? } >I think you can get rid of this "reject_if" option since you have a "validates_presence_of :text" in your Question controller.> attr_accessible :name, :description > > end > > > class Question < ActiveRecord::Base > > belongs_to :survey > > acts_as_list :scope => :survey > > validates_presence_of :text, :message => ''Text can not be blank'' > > attr_accessible :survey_id, :text > > end > > > I get this error when I run the test.rb script (see code snippet above) ... > > /Users/dse/.rvm/gems/ruby-1.9.3-p327@sq/gems/activerecord-3.2.9/lib/active_record/associations/association.rb:204:in > `raise_on_type_mismatch'': Question(#70357035179920) expected, got > Hash(#70357017137920) (ActiveRecord::AssociationTypeMismatch) > > > Any help would be greatly appreciated. > > > Thanks again > > Dave > > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/ngpKVHYpMVoJ. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- Nicolas Desprès -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
On Tuesday, 18 December 2012 23:30:57 UTC-5, dekhaus wrote:> > > For reference = here is the ruby code for the Survey and Question classes > ... > > class Survey < ActiveRecord::Base > > has_many :questions, :dependent => :destroy > > accepts_nested_attributes_for :questions, :allow_destroy => true, > > :reject_if => lambda {|attrs| > attrs[''text''].blank? } > > attr_accessible :name, :description > > end > > [snip]> I get this error when I run the test.rb script (see code snippet above) ... > > /Users/dse/.rvm/gems/ruby-1.9.3-p327@sq/gems/activerecord-3.2.9/lib/active_record/associations/association.rb:204:in > `raise_on_type_mismatch'': Question(#70357035179920) expected, got > Hash(#70357017137920) (ActiveRecord::AssociationTypeMismatch) >accepts_nested_attributes creates a writer method on Survey, but it''s not ''questions'' (which is the association) but rather ''questions_attributes''. --Matt Jones -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/sJU0WqSc4nEJ. For more options, visit https://groups.google.com/groups/opt_out.