创建具有关联的 Ruby ActiveRecord 对象
我有具有地址(多态关联)的人员和团队,
我需要在代码中分别创建人员和地址:
person = Person.new
address = Address.new
然后使用类似以下内容将它们链接在一起:
<代码> person.addressable.push(地址)
最后,执行:
人.救!
然而,当您没有预料到时,这会给出 You have a nil object! 评估 nil.has_key 时发生错误?
有人可以让我知道我需要做什么吗?谷歌上几乎没有代码可以演示这些内容,我能找到的只是 API 参考
I have Persons and Teams that have Addresses (polymorphic association)
I need to create persons and addresses separately in code:
person = Person.new
address = Address.new
and then link them together using something like:
person.addressable.push(address)
finally, do:
person.save!
This however gives a You have a nil object when you didn't expect it!
The error occurred while evaluating nil.has_key?
Could someone let me know what I need to be doing? There is little code on google that demoes any of this and all I could find was the API reference
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题 - 我已经覆盖了 Address 类中的初始化。发现这是最愚蠢的事情:-(
Found the issue - I had overridden initialize in the Address class. Figured out it was the dumbest thing to do :-(