Rails 5 上 Ruby 中的 build() 发生了什么
What happened to build() in Ruby on Rails 5
我在 Rails 4 / 5 中使用嵌套表单。在 Rails4 中使用 @my_model_instance.build
的代码似乎在 Rails 5 中不起作用.不知道是bug还是我的问题
详情:
Rails 4.2.3 控制台:
> @item = ItemType.new
=> #<ItemType id: nil, name: nil, .... >
> @item.item_abilities
=> #<ActiveRecord::Associations::CollectionProxy []>
> @item.item_abilities.build
=> #<ItemAbility id: nil, item_type_id: nil, ... >
> @item.item_abilities
=> #<ActiveRecord::Associations::CollectionProxy [#<ItemAbility id: nil, item_type_id: nil, ... ]>
这相当于 documented/expected。现在让我们在 Rails 5 (rc2).
中的相同代码库上尝试相同的东西
Rails 5.0.0rc2 控制台:
> @item = ItemType.new
=> #<ItemType id: nil, name: nil, .... >
> @item.item_abilities
=> #<ActiveRecord::Associations::CollectionProxy []>
> @item.item_abilities.build
ArgumentError: wrong number of arguments (given 2, expected 0..1)
from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.rc2/lib/active_record/core.rb:312:in `initialize'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.rc2/lib/active_record/inheritance.rb:65:in `new'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.rc2/lib/active_record/inheritance.rb:65:in `new'
from /var/lib/gems/2.3.0/gems/protected_attributes-1.1.3/lib/active_record/mass_assignment_security/reflection.rb:8:in `build_association'
from /var/lib/gems/2.3.0/gems/protected_attributes-1.1.3/lib/active_record/mass_assignment_security/associations.rb:7:in `build_record'
from /var/lib/gems/2.3.0/gems/protected_attributes-1.1.3/lib/active_record/mass_assignment_security/associations.rb:25:in `build'
from /var/lib/gems/2.3.0/gems/protected_attributes-1.1.3/lib/active_record/mass_assignment_security/associations.rb:64:in `build'
from (irb):2
from /var/lib/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/console.rb:65:in `start'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/console_helper.rb:9:in `start'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/commands_tasks.rb:78:in `console'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
这是我在做的事情,还是 Rails5 疯了?
模型相关栏目:
item_type.rb
class ItemType < ActiveRecord::Base
## Relationships
belongs_to :item_collection
has_many :item_abilities
has_many :item_instances
accepts_nested_attributes_for :item_abilities
...
end
item_instance.rb
class ItemAbility < ActiveRecord::Base
# Relationships
belongs_to :item_type
...
end
Ruby版本:
ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]
是 protected_attributes
gem,在 Rails 3->4 天后仍然徘徊,导致了上述问题。
我在 protected_attributes
gem 网站上发布了一个错误,他们回复说 gem 将在 Rails 5.
中不受支持
如果其他人遇到此问题,Rails 'proper' 可以在此处找到解决此问题的方法:How is attr_accessible used in Rails 4? or here: http://api.rubyonrails.org/classes/ActionController/StrongParameters.html
感谢@oreoluwa 为我指明了正确的方向
我在 Rails 4 / 5 中使用嵌套表单。在 Rails4 中使用 @my_model_instance.build
的代码似乎在 Rails 5 中不起作用.不知道是bug还是我的问题
详情:
Rails 4.2.3 控制台:
> @item = ItemType.new
=> #<ItemType id: nil, name: nil, .... >
> @item.item_abilities
=> #<ActiveRecord::Associations::CollectionProxy []>
> @item.item_abilities.build
=> #<ItemAbility id: nil, item_type_id: nil, ... >
> @item.item_abilities
=> #<ActiveRecord::Associations::CollectionProxy [#<ItemAbility id: nil, item_type_id: nil, ... ]>
这相当于 documented/expected。现在让我们在 Rails 5 (rc2).
中的相同代码库上尝试相同的东西Rails 5.0.0rc2 控制台:
> @item = ItemType.new
=> #<ItemType id: nil, name: nil, .... >
> @item.item_abilities
=> #<ActiveRecord::Associations::CollectionProxy []>
> @item.item_abilities.build
ArgumentError: wrong number of arguments (given 2, expected 0..1)
from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.rc2/lib/active_record/core.rb:312:in `initialize'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.rc2/lib/active_record/inheritance.rb:65:in `new'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.0.rc2/lib/active_record/inheritance.rb:65:in `new'
from /var/lib/gems/2.3.0/gems/protected_attributes-1.1.3/lib/active_record/mass_assignment_security/reflection.rb:8:in `build_association'
from /var/lib/gems/2.3.0/gems/protected_attributes-1.1.3/lib/active_record/mass_assignment_security/associations.rb:7:in `build_record'
from /var/lib/gems/2.3.0/gems/protected_attributes-1.1.3/lib/active_record/mass_assignment_security/associations.rb:25:in `build'
from /var/lib/gems/2.3.0/gems/protected_attributes-1.1.3/lib/active_record/mass_assignment_security/associations.rb:64:in `build'
from (irb):2
from /var/lib/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/console.rb:65:in `start'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/console_helper.rb:9:in `start'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/commands_tasks.rb:78:in `console'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
这是我在做的事情,还是 Rails5 疯了?
模型相关栏目:
item_type.rb
class ItemType < ActiveRecord::Base
## Relationships
belongs_to :item_collection
has_many :item_abilities
has_many :item_instances
accepts_nested_attributes_for :item_abilities
...
end
item_instance.rb
class ItemAbility < ActiveRecord::Base
# Relationships
belongs_to :item_type
...
end
Ruby版本:
ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]
是 protected_attributes
gem,在 Rails 3->4 天后仍然徘徊,导致了上述问题。
我在 protected_attributes
gem 网站上发布了一个错误,他们回复说 gem 将在 Rails 5.
如果其他人遇到此问题,Rails 'proper' 可以在此处找到解决此问题的方法:How is attr_accessible used in Rails 4? or here: http://api.rubyonrails.org/classes/ActionController/StrongParameters.html
感谢@oreoluwa 为我指明了正确的方向