我一直在努力ActiveRecord探索协会,但我已经碰到了一些砖墙,无论我查看ActiveRecord文档多少,我都无法解决如何解决问题.
我有两节课:
Property -> has_one :contract
Contract -> belongs_to :property
Run Code Online (Sandbox Code Playgroud)
在我的契约类中,我有一个create_or_update_from_xml的方法
首先,我检查以确保有问题的财产存在.
property_unique_id = xml_node.css('property_id').text
property = Property.find_by_unique_id(property_unique_id)
next unless property
Run Code Online (Sandbox Code Playgroud)
这就是我遇到困难的地方,我有合同的属性哈希,我想要做的是:
if property.contract.nil?
# create a new one and populate it with attributes
else
# use the existing one and update it with attributes
Run Code Online (Sandbox Code Playgroud)
我知道如果它是原始SQL我会怎么做,但我无法理解主动的ActiveRecord方法.
任何通过这个路障的提示都将受到极大的赞赏.
提前致谢.