Rails 3 Accepted_nested_attributes_for 似乎正在生成“警告:无法批量分配受保护的属性:”
我有以下两个模型:
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_at
或 updated_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论