Error creating model object using accepts_nested_attributes_for

Folks,

I am on Rails 2.3.8. I have models declared as below

class GradeSection < ActiveRecord::Base
has_many :class_calendars

end

class ClassCalendar < ActiveRecord::Base
has_many :uploaded_attachments, :class_name => ‘UploadedAttachment’
accepts_nested_attributes_for :uploaded_attachments
belongs_to :grade_section
end

class UploadedAttachment < ActiveRecord::Base
belongs_to :class_calendar
has_attached_file :phile,
:url => “/assets/class_cal/:id/:style/:basename.:extension”
end

In my create function in GradeSection controller I get data posted as
such

Processing GradeSectionController#create (for 127.0.0.1 at 2010-11-27
22:13:46) [POST]
Parameters:
{“authenticity_token”=>“fwdgGanIsScNQUcm6sYc952xhT2BSVH6LXggOJdxhKo=”,
“class_calendar”=>{“dt”=>“Mon Dec 13 2010 22:13:35 GMT-0800 (Paci
fic Standard Time)”, “summary”=>"", “grade_section_id”=>“1”,
“chkproj”=>“0”, “uploaded_attachments”=>{“name”=>“Test 2”},
“chkhw”=>“1”}}

“name” is a string field for filename in UploadedAttachments model (I
am going to use that model for storing uploaded attachments but taking
incremental steps - getting just the name for now) and
grade_section_id is passed in so I create the model through
ClassCalendar

When I get the above posted data, in the create function of
GradeSectionController, I create a ClassCalendar object from params
that are submitted as below

@cc = ClassCalendar.new(params[:class_calendar]) # line 96 in the
code

But this line gives me the following error

#================= Error Message =======================
ActiveRecord::AssociationTypeMismatch (UploadedAttachment(#90313070)
expected, got Array(#1200170)):
app/controllers/grade_section_controller.rb:96:in new' app/controllers/grade_section_controller.rb:96:increate’
c:/ruby/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.5/lib/ruby-
debug.rb:101:in debug_load' c:/ruby/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.5/lib/ruby- debug.rb:101:indebug_program’
c:/ruby/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.5/bin/rdebug-ide:
82
c:\ruby\bin\rdebug-ide:19:in load' c:\ruby\bin\rdebug-ide:19 -e:2:inload’
-e:2
#================= end error message =======================

I may be missing something very simple but I don’t understand the
error and don’t quite know how to debug this - getting an Array
instead of UploadedAttachment. Looking for help with any pointers on
what the issue is and how to debug such errors.

Thanks,
-S

Looking for any pointer to help address or some way to debug this
problem. Appreciate any thoughts.

Thanks,
-S

On 28 November 2010 22:08, skt [email protected] wrote:

has_many :uploaded_attachments, :class_name => ‘UploadedAttachment’
In my create function in GradeSection controller I get data posted as
such

Processing GradeSectionController#create (for 127.0.0.1 at 2010-11-27
22:13:46) [POST]
Parameters:
{“authenticity_token”=>“fwdgGanIsScNQUcm6sYc952xhT2BSVH6LXggOJdxhKo=”,
“class_calendar”=>{“dt”=>“Mon Dec 13 2010 22:13:35 GMT-0800 (Paci
fic Standard Time)”, “summary”=>“”, “grade_section_id”=>“1”,
“chkproj”=>“0”, “uploaded_attachments”=>{“name”=>“Test 2”},

Since no one else has responded, and I don’t know how it relates to
the error you are seeing, but I wonder whether this should be an array
of uploaded_attachments rather than a single instance.

Whether that is the issue or not, it might be worth removing the
attachements spec from the form initially just to check that you can
create a new object without the nested objects.

that are submitted as below
app/controllers/grade_section_controller.rb:96:in `create’
#================= end error message =======================

I may be missing something very simple but I don’t understand the
error and don’t quite know how to debug this - getting an Array
instead of UploadedAttachment. Looking for help with any pointers on
what the issue is and how to debug such errors.

Do you know how to use reby-debug to break into your code and inspect
data and follow flow. You could break in before the offending line to
double check the data at that point. See the Rails Guide on debugging
if you are not familiar with this.

Colin

On Nov 28, 10:08pm, skt [email protected] wrote:

“chkhw”=>“1”}}

“name” is a string field for filename in UploadedAttachments model (I
am going to use that model for storing uploaded attachments but taking
incremental steps - getting just the name for now) and
grade_section_id is passed in so I create the model through
ClassCalendar

This data doesn’t look like what the nested_attributes stuff looks
like - I would expected it to be more like
‘uploaded_attachments_attributes’ => {{‘0’ => {‘name’ => ‘Test 2’}}

What does your form look like?

Fred

Thanks Frank. I finally found this post on the forum
Problems with accepts_nested_attributes_for - Rails - Ruby-Forum that points out what you have. It
is interesting that the form_for declaration in the view has to be
something like follows

<% form_for @modelname do |f| %>

instead of

<% form_for :modelname do |f| %>

Don’t yet understand why this makes a difference but that is what makes
the difference and posts data with nested model name appended with
“attributes” as you point below.

Thanks for the feedback - very much appreciated. Past that hurdle and
onto the next one.

-S

Frederick C. wrote in post #965073:

On Nov 28, 10:08pm, skt [email protected] wrote:

“chkhw”=>“1”}}

“name” is a string field for filename in UploadedAttachments model (I
am going to use that model for storing uploaded attachments but taking
incremental steps - getting just the name for now) and
grade_section_id is passed in so I create the model through
ClassCalendar

This data doesn’t look like what the nested_attributes stuff looks
like - I would expected it to be more like
‘uploaded_attachments_attributes’ => {{‘0’ => {‘name’ => ‘Test 2’}}

What does your form look like?

Fred