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

发布于 2024-11-29 04:22:56 字数 1751 浏览 0 评论 0原文

我有以下两个模型:

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

我的头寸模型架构是:

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 进行任何更改。

I have the following two models:

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

and

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

My schema for the Positions model is:

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

If I "post" (update) a Trade_Set record with three Position records I get the following warning in the log:

  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

Am I somehow not using accepts_nested_attributes_for correctly? I'm not trying to make any changes to created_at or updated_at anywhere in my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文