Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails验证在更新中接受\u嵌套的\u属性,\u和habtm关联重置关联_Ruby On Rails_Validation_Has And Belongs To Many_Update Attributes - Fatal编程技术网

Ruby on rails rails验证在更新中接受\u嵌套的\u属性,\u和habtm关联重置关联

Ruby on rails rails验证在更新中接受\u嵌套的\u属性,\u和habtm关联重置关联,ruby-on-rails,validation,has-and-belongs-to-many,update-attributes,Ruby On Rails,Validation,Has And Belongs To Many,Update Attributes,在rails 2.3.8中,如果我在验证过程中循环甚至检查habtm关联,那么在接受嵌套属性关联的情况下,我在验证时会遇到问题,课程会被加载,任何更新都会丢失。例如,我有以下模式: ActiveRecord::Schema.define(:version => 20100829151836) do create_table "attendees", :force => true do |t| t.integer "lesson_set_id" t.string "n

在rails 2.3.8中,如果我在验证过程中循环甚至检查habtm关联,那么在接受嵌套属性关联的情况下,我在验证时会遇到问题,课程会被加载,任何更新都会丢失。例如,我有以下模式:

ActiveRecord::Schema.define(:version => 20100829151836) do

 create_table "attendees", :force => true do |t|
  t.integer  "lesson_set_id"
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
 end

 create_table "lesson_sets", :force => true do |t|
  t.datetime "created_at"
  t.datetime "updated_at"
 end 

 create_table "lesson_sets_lessons", :id => false, :force => true do |t|
  t.integer "lesson_id"
  t.integer "lesson_set_id"
 end

 create_table "lessons", :force => true do |t|
  t.integer  "activity_id"
  t.integer  "limit"
  t.datetime "created_at"
  t.datetime "updated_at"
 end
end
以及以下型号:

class Attendee < ActiveRecord::Base
 belongs_to :lesson_sets
end
class Lesson < ActiveRecord::Base
 has_and_belongs_to_many :lesson_sets
end
class LessonSet < ActiveRecord::Base
 has_and_belongs_to_many :lessons
 has_many :attendees
 accepts_nested_attributes_for :lessons
 validate do |lesson_set|
  lesson_set.lessons.each do |l| 
   if l.limit < attendees.count
    self.errors.add_to_base("Sorry only #{l.limit} people allowed on lesson")
   end
  end
 end
end
课堂参与者
如果我称之为:

>> ls = LessonSet.first
=> #<LessonSet id: 1, created_at: "2010-08-29 07:50:23", updated_
at: "2010-08-29 07:50:23">
>> Lesson.last
=> #<Lesson id: 1, activity_id: 29, created_at: "2010-08-29 07:57:13", updated_a
t: "2010-08-29 17:04:34", limit: 5>
params = {"lessons_attributes"=>{"0"=>{"activity_id"=>"3", "id"=>"1", "_destroy"=>""}}, "id"=>"1"}
=> {"id"=>"1", "lessons_attributes"=>{"0"=>{"activity_id"=>"3", "id"=>"1", "_des
troy"=>""}}}
>> ls.update_attributes(params)
=> true
>> Lesson.last
=> #<Lesson id: 1, activity_id: 29, created_at: "2010-08-29 07:57:13", updated_a
t: "2010-08-29 17:04:34", limit: 5>
>> params = {"lessons_attributes"=>{"0"=>{"activity_id"=>"29", "id"=>"1", "_de>
=> {"id"=>"1", "lessons_attributes"=>{"0"=>{"activity_id"=>"29", "id"=>"1", "_de
stroy"=>""}}}
>> ls.update_attributes(params)
=> true
<y_id"=>"3", "id"=>"1", "_destroy"=>""}}, "id"=>"1"}
=> {"id"=>"1", "lessons_attributes"=>{"0"=>{"activity_id"=>"3", "id"=>"1", "_des
troy"=>""}}}
>> Lesson.last
=> #<Lesson id: 1, activity_id: 29, created_at: "2010-08-29 07:57:13", updated_a
t: "2010-08-29 17:04:34", limit: 5>
>> ls.update_attributes(params)
=> true
>> Lesson.last
=> #<Lesson id: 1, activity_id: 3, created_at: "2010-08-29 07:57:13", updated_at
: "2010-08-29 17:13:36", limit: 5>
>ls=LessonSet.first
=> #
>>最后一课
=> #
参数={“课程属性”=>{“0”=>{“活动id”=>“3”,“id”=>“1”,“销毁”=>“}}”,id“=>“1”}
=>{“id”=>“1”,“课程属性”=>{“0”=>{“活动id”=>“3”,“id”=>“1”,“课程属性”
特洛伊“=>”}}
>>ls.更新_属性(参数)
=>正确
>>最后一课
=> #
>>参数={“课程属性”=>{“0”=>{“活动id”=>“29”,“id”=>“1”,“de>
=>{“id”=>“1”,“课程属性”=>{“0”=>{“活动id”=>“29”,“id”=>“1”,“\u de”
stroy“=>”}}
>>ls.更新_属性(参数)
=>正确

使用上述型号
课程有并且属于许多课程集
与会者属于课程集

尝试验证参与者不应超过限制意味着在保存新关联之前,在验证期间重置了课程和课程集之间的关联

将验证逻辑从课程集移动到与会者可以使验证工作正常

因此,如果您有类似的问题,请尝试将验证逻辑从连接模型移动到它连接的其中一个模型