Rails 3.1 - 使用日期保存多个模型

时间:2011-06-29 03:35:51

标签: mysql ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1

我有一个嵌套表单设置,允许我一次创建最多7个“Schedule”实例。每个实例都允许用户分配schedule_start_time(datetime),schedule_end_time(datetime)和notes(String)字段值。当我提交表格并完成其中一些时,参数数组看起来与我期望的完全一样:

  

{ “UTF8”=> “中✓”,   “authenticity_token”=> “中HEoylzovRgr7BCZH47iNRPfizDHeVFMLTEmIiNeudcw =”,   “锻炼”=> { “ID”=> “中2”,   “schedules_attributes”=> {

     

“0”=> { “scheduled_start_time”=> “中06/01/2011”,   “scheduled_end_time”=> “中2011年6月2日”,   “notes”=>“Notes 1”},

     

“1”=> { “scheduled_start_time”=> “中”,   “scheduled_end_time”=> “中”,   “注释”=> “中”},

     

“2”=> { “scheduled_start_time”=> “中06/03/2011”,   “scheduled_end_time”=> “中06/04/2011”,   “notes”=>“Notes 2”},

     

“3”=> { “scheduled_start_time”=> “中”,   “scheduled_end_time”=> “中”,   “注释”=> “中”},

     

“4”=> { “scheduled_start_time”=> “中06月16日”,   “scheduled_end_time”=> “中06/30/2011”,   “notes”=>“Notes 3”},

     

“5”=> { “scheduled_start_time”=> “中”,   “scheduled_end_time”=> “中”,   “注释”=> “中”},

     

“6”=> { “scheduled_start_time”=> “中”,   “scheduled_end_time”=> “中”,   “notes”=>“”}}},“commit”=>“提交”}

在控制器中,我使用空白start_date过滤那些“计划”。我的params列表看起来像这样:

  

{ “UTF8”=> “中✓”,   “authenticity_token”=> “中HEoylzovRgr7BCZH47iNRPfizDHeVFMLTEmIiNeudcw =”,   “锻炼”=> { “ID”=> “中2”,   “schedules_attributes”=> {

     

“0”=> { “scheduled_start_time”=> “中06/01/2011”,   “scheduled_end_time”=> “中2011年6月2日”,   “notes”=>“Notes 1”},

     

“2”=> { “scheduled_start_time”=> “中06/03/2011”,   “scheduled_end_time”=> “中06/04/2011”,   “notes”=>“Notes 2”},

     

“4”=> { “scheduled_start_time”=> “中06月16日”,   “scheduled_end_time”=> “中06/30/2011”,   “notes”=>“Notes 3”}}},

     

“提交”=> “中提交”,   “行动”=> “中创建”,   “控制器”=> “中的时间表”}

生成的SQL并不是我所期望的:

  

(0.1ms)BEGIN警告:不能   mass-assign protected attributes:id

     

SQL(0.2ms)INSERT INTO schedules   (created_atnotes,   scheduled_end_time,   scheduled_start_timeupdated_at,   workout_id)VALUES('2011-06-29   03:23:45','注1','2011-02-06   00:00:00','2011-01-06 00:00:00',   '2011-06-29 03:23:45',2)

     

SQL   (0.1ms)INSERT INTO schedules   (created_atnotes,   scheduled_end_time,   scheduled_start_timeupdated_at,   workout_id)VALUES('2011-06-29   03:23:45','Notes 2','2011-04-06   00:00:00','2011-03-06 00:00:00',   '2011-06-29 03:23:45',2)

     

SQL   (0.2ms)INSERT INTO schedules   (created_atnotes,   scheduled_end_time,   scheduled_start_timeupdated_at,   workout_id)VALUES('2011-06-29   03:23:45','注3',NULL,NULL,   '2011-06-29 03:23:45',2)(0.5ms)   COMMIT

一些有效的日期值在params数组中,但在SQL提交之前被过滤掉了。

这是控制器代码:

  def create
    @workout = Workout.find(params[:workout][:id])

    7.times do |count|
      @schedule = params[:workout][:schedules_attributes]["#{count}"]
      if (@schedule[:scheduled_start_time].blank?)
        params[:workout][:schedules_attributes].delete count.to_s.to_sym
      end
    end

    if @workout.update_attributes(params[:workout])
      redirect_to schedules_url, :notice  => "Successfully updated schedule."
    else
      render :action => 'new'
    end
  end

锻炼模型

class Workout < ActiveRecord::Base

belongs_to :team, :class_name => "Team", :foreign_key => "team_id"
has_many :exercise_instances, :dependent => :destroy

validates :name,
          :presence => true

has_many :schedules, :dependent => :destroy
accepts_nested_attributes_for :schedules

end

和计划模型

class Schedule < ActiveRecord::Base
  attr_accessible :workout_id, :scheduled_start_time, :scheduled_end_time, :notes
  belongs_to :workout
end

欢迎任何方向。我怀疑某个级别的缓存,但我不知道从哪里开始寻找。谢谢!

2 个答案:

答案 0 :(得分:1)

就个人而言,我会做类似

的事情
params[:workout][:schedules_attributes].each do |sched|
  @workout.schedules << @workout.schedules.build(sched) if sched[:scheduled_start_time].present?
end

if @workout.save # etc

不要使用nested_attributes_for。这将保证您只能获得发送的内容。

我发现使用nested_attributes时,通常最好每次都删除并重新编辑,这可能是也可能不是你想要的。

我确信拥有比我好的nested_attributes'fu'的其他人可能有更好的解决方案。

答案 1 :(得分:1)

我认为你在这里重新发明轮子,这是问题的根源。请尝试以下方法:

class Workout < ActiveRecord::Base
  has_many :schedules, :dependent => :delete_all
  accepts_nested_attributes_for :schedules, :allow_destroy => true, :reject_if => proc { |schedule| schedule[:scheduled_start_time].blank? }
end

您仍然需要将计划数量限制为7.我建议您检查此行为的现有解决方案;这已经有很多可靠的模式了。