Forum: Ruby on Rails ActiveRecord object from json

Posted by dekhaus (Guest)
on 2012-12-19 05:32
(Received via mailing list)
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
Posted by "Nicolas Desprès" <nicolas.despres@gmail.com> (Guest)
on 2012-12-19 10:49
(Received via mailing list)
On Wed, Dec 19, 2012 at 5:30 AM, dekhaus <dekhaus@mac.com> wrote:

> Hi All
>
> Hi,

>
> # 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

> survey = Survey.new
>
> survey.attributes = ActiveSupport::JSON.decode(json)
>
survey = Survey.new(ActiveSupport::JSON.decode(json))

I have never tried with survey.attributes.

>  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.

>
> `raise_on_type_mismatch': Question(#70357035179920) expected, got
>
>
>



--
Nicolas Desprs
Posted by Matt Jones (Guest)
on 2012-12-19 23:00
(Received via mailing list)
On Tuesday, 18 December 2012 23:30:57 UTC-5, dekhaus wrote:
>
>                                :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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.