“找不到”错误 ActiveRecord.new 方法
我在 5 月的 Rails 3.0.3 应用程序中遇到了这个问题,我认为这是一个愚蠢的错误,但我无法弄清楚为什么会发生这种情况,或者事实上,我误解了 ActiveRecord 的行为,这并不是真正的错误。
这是场景,我有三个模型:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :addresses, :as => :addressable
accepts_nested_attributes_for :addresses
end
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
belongs_to :address_base
accepts_nested_attributes_for :address_base
end
class AddressBase < ActiveRecord::Base
has_many :address
end
如果我尝试实例化一个传递散列参数的新用户,通过这种方式:
User.new({"addresses_attributes"=>
{"0"=>
{"number"=>"10",
"complement"=>"Next Starbucks",
"address_base_attributes"=>
{"city"=>"San Francisco",
"zip_code"=>"00010",
"district"=>"San Francisco",
"id"=>"10",
"street"=>"Market St.",
"state"=>"CA"}
}
},
"name"=>"Homer Simpson",
"password_confirmation"=>"[FILTERED]",
"document"=>"123321111",
"password"=>"[FILTERED]",
"email"=>"[email protected]"
})
我面临错误
Couldn't find AddressBase with ID=10 for Address with ID=
发生这种情况是因为 AddressBase 已经存在,而 Address 不存在,如果我删除哈希的AddressBase.id参数一切正常,但我不想要它,因为这样,总是会为Address和AddressBase创建一个新的寄存器。我的目的是重用公共地址库,因此有必要使用现有地址库创建新地址。
现在这是我的疑问,我在 AddressBase 哈希中缺少一些参数?比如说该记录已经存在?或者是ActiveRecord的belongs_to和has_many关联有问题?
提前致谢。
I facing this problem in may Rails 3.0.3 App, I think this is a silly error but I can't figure out why it's happening, or in fact, I'm misunderstanding the ActiveRecord behavior and it's is not really a error.
This is the scenario, I've three models:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :addresses, :as => :addressable
accepts_nested_attributes_for :addresses
end
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
belongs_to :address_base
accepts_nested_attributes_for :address_base
end
class AddressBase < ActiveRecord::Base
has_many :address
end
If I try to instantiate a new User passing a hash parameters, by this way:
User.new({"addresses_attributes"=>
{"0"=>
{"number"=>"10",
"complement"=>"Next Starbucks",
"address_base_attributes"=>
{"city"=>"San Francisco",
"zip_code"=>"00010",
"district"=>"San Francisco",
"id"=>"10",
"street"=>"Market St.",
"state"=>"CA"}
}
},
"name"=>"Homer Simpson",
"password_confirmation"=>"[FILTERED]",
"document"=>"123321111",
"password"=>"[FILTERED]",
"email"=>"[email protected]"
})
I face the error
Couldn't find AddressBase with ID=10 for Address with ID=
And it happens because that AddressBase already exists and the Address don't, if I remove the AddressBase.id parameter of the hash everything works, but I not want it, because on this way, always will be created a new register for Address and AddressBase. My intention is reuse commons AddressBase's, so the scenario of a new Address with an existent AddressBase will be necessary.
Now this is my doubts, I'm missing some parameter in the AddressBase hash?? Something like saying that the record already exist? Or it's a problem with belongs_to and has_many associations of ActiveRecord?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您无法在 Rails 模型中批量分配 ID 属性(将 ID 赋予用于初始化模型的数据哈希)。
我很难重现您的情况,但如果您知道模型存在,那么提供 address_base 的 id 而不是包含所有数据的哈希会更容易。
例如:
})
The problem is that you cannot mass assign the ID attribute in rails models (give the ID to the data hash you initialize the model with).
It's hard for me to reproduce you're situation but if you know the model exist it's easier to supply the id of address_base instead of a hash with all the data.
For example:
})