Ruby on Rails Tuesday, November 30, 2010

On 28 November 2010 22:08, skt <stibrewal@gmail.com> wrote:
> Folks,
>
> I am on Rails 2.3.8. I have models declared as below
>
> class GradeSection < ActiveRecord::Base
>  has_many  :class_calendars
>  <more stuff>
> 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"},

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.

> "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:in `create'
>  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:in `debug_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:in `load'
>  -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.

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

--
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@googlegroups.com.
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.

No comments:

Post a Comment