【发布时间】:2015-02-13 22:17:19
【问题描述】:
Rails 不希望存储嵌套对象的更改属性。我用:
- 导轨 4
- Postgresql 9.3
- Postgresql 中的 Hstore 列
- 视图中的嵌套属性
- 与 rolify gem 一起使用
这里是数据库表:
create_table :accounts_roles do |t|
t.references :account, null: false
t.references :role, null: false
t.hstore :configurations
t.timestamps
end
这里是模型:
class Account < ActiveRecord::Base
has_many :accounts_roles
has_many :roles, through: :accounts_roles
accepts_nested_attributes_for :accounts_roles
end
class AccountsRole < ActiveRecord::Base
belongs_to :account
belongs_to :role
store_accessor :configurations, :color
end
控制器允许调试所有属性:
def account_params
params.require(:account).permit!
end
在我看来,我使用 fields_for:
<%= form_for @account ... %>
...
<%= f.fields_for :accounts_roles do |ar| %>
<%= ar.text_field :color ... %>
如果我提交编辑表单,参数哈希看起来很好:
{"utf8"=>"✓", "account"=>{..., "accounts_roles_attributes"=>{"0"=>{"color"=>"green", "id"=>"5"}}},...}
但是颜色没有变成“绿色”,还是“红色”!
在 Rails 控制台中,我尝试手动执行相同的过程:
> u = Account.first
> u.account_roles.first.color
=> "red"
> u.update(accounts_roles_attributes: { color: "green", id: 5 } )
=> true
> u.account_roles.first.color
=> "green"
> u.save
=> true
> u = Account.first
> u.account_roles.first.color
=> "red"
但没有成功。属性颜色保持“红色”。 有什么想法吗?
【问题讨论】:
-
您是否尝试重新启动服务器/控制台?另外,尝试使用 bang 方法:保存!更新!在尝试调试时。虽然更新/保存返回 true,但检查记录是否已更改?和/或 .persisted?在那些之后。您还可以检查 postgresql 日志以检查特定事务。
-
是的,我已经多次重启服务器和控制台。我在日志中看不到更新
accounts_roles表的SQL 语句!就像,rails看不到提交的属性和数据库中的不一样。 -
不确定是否可能是这种情况,但 .update 可能会在用户对象实际更新时返回 true - 嵌套对象可能不是这种情况?更新后,当您要求
u.account_roles.first.color时,您并没有访问数据库 - 您正在获取刚刚设置的对象的属性。 -
在此示例中的
u.save之后(参见控制台示例),我使用u = Account.first重新加载对象以查看数据库是否有更新。如果我这样做:ar = u.account_roles.first,然后是ar.color = 'green'和ar.save,那么我会看到更新 SQL 转换。这行得通。因此,这不是验证问题。因为它以这种方式工作。但是为什么不能在带有嵌套属性的视图中工作呢?
标签: ruby-on-rails-4 nested-attributes has-many rails-postgresql hstore