ActiveRecord object from json

87 views
Skip to first unread message

dekhaus

unread,
Dec 18, 2012, 11:30:57 PM12/18/12
to rubyonra...@googlegroups.com
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


Nicolas Desprès

unread,
Dec 19, 2012, 4:48:15 AM12/19/12
to rubyonra...@googlegroups.com


On Wed, Dec 19, 2012 at 5:30 AM, dekhaus <dek...@mac.com> 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 rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
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

Matt Jones

unread,
Dec 19, 2012, 4:58:44 PM12/19/12
to rubyonra...@googlegroups.com


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
Reply all
Reply to author
Forward
0 new messages