0

i have two objects - material - lesson

each material can have and belong to lesson; each lesson can have and belong to material.

in material_controller when i try to create

@material = Material.new(params[:material])

class Material < ActiveRecord::Base
   has_and_belongs_to_many :lessons

   attr_accessible :content_type, :user_id, :lesson_ids

here is params

"material"=>{"content_type"=>"2",
"detail_content"=>"",
"user_id"=>"5",
"lesson_ids"=>"[]"},

create_table "lessons", :force => true do |t|
t.string   "title"
t.string   "description"
t.integer  "course_id"
t.integer  "sequence"
t.datetime "created_at",  :null => false
t.datetime "updated_at",  :null => false
end

create_table "lessons_materials", :force => true do |t|
t.integer "lesson_id"
t.integer "material_id"
end

create_table "materials", :force => true do |t|
t.integer  "content_type"
t.text     "detail_content"
t.text     "embedded_content"
t.string   "stored_file_name"
t.string   "stored_content_type"
t.integer  "stored_file_size"
t.datetime "stored_updated_at"
t.datetime "created_at",          :null => false
t.datetime "updated_at",          :null => false
t.integer  "user_id"
end

1 Answers1

0

I think your lesson_ids param should be nil when you have no lesson associated, instead of being an array.

m_x
  • 12,357
  • 7
  • 46
  • 60
  • but what about when it has value, that doesn't work either. e.g lesson_ids = [6] – Rajesh Bhatia Nov 10 '12 at 14:16
  • maybe we should also see your migrations for Material and Lesson – awenkhh Nov 10 '12 at 18:32
  • class Material < ActiveRecord::Base has_and_belongs_to_many :lessons attr_accessible :content_type, :detail_content, :embedded_content, :stored, :user_id, :lesson_ids has_attached_file :stored validates_presence_of :content_type, :user_id validates_inclusion_of :content_type, :in => 1..3 – Rajesh Bhatia Nov 11 '12 at 02:32