Rails 3 accepts_nested_attributes_for似乎正在生成“警告:无法批量分配受保护的属性:”

时间:2011-08-10 02:58:47

标签: ruby-on-rails ruby-on-rails-3 nested-attributes

我有以下两种模式:

class Position < ActiveRecord::Base
  include Stocks
  belongs_to :trade_set
  attr_accessible :symbol, :percent, :shares, :open, :close, :trade_set_id, :name

  def set_stock_info(symbol, name)
    self.name = name
    self.symbol = symbol
  end

  def set_position_values(percent, shares, open, close)
    self.percent  = percent
    self.shares  = shares
    self.open  = open
    self.close  = close
  end

class Trade_Set < ActiveRecord::Base
  belongs_to :user
  has_many :positions, :dependent => :destroy
  has_many :comments, :dependent => :destroy
  belongs_to :trading_period
  accepts_nested_attributes_for :positions, :allow_destroy => true
  attr_accessible :workflow_state, :cash, :open, :close, :title,
                  :description, :log, :positions_attributes, :trading_period_id
  #...
end

我对Positions模型的架构是:

create_table "positions", :force => true do |t|
    t.string   "symbol"
    t.integer  "percent"
    t.integer  "shares"
    t.decimal  "open"
    t.decimal  "close"
    t.integer  "trade_set_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "name"
  end

如果我“发布”(更新)具有三个位置记录的Trade_Set记录,我会在日志中收到以下警告:

  Position Load (2.8ms)  SELECT "positions".* FROM "positions" WHERE ("positions".trade_set_id = 7)
WARNING: Can't mass-assign protected attributes: created_at, updated_at
WARNING: Can't mass-assign protected attributes: created_at, updated_at
WARNING: Can't mass-assign protected attributes: created_at, updated_at

我不知道怎么没有正确使用accepts_nested_attributes_for?我不是要在代码中的任何位置对created_atupdated_at进行任何更改。

0 个答案:

没有答案