我一直在试图让我的脑袋围绕ActiveRecord
协会,但是我碰到了一堵砖墙,不管我多么复习ActiveRecord
文档,我都无法工作如何解决我的问题。创建或更新has_one ActiveRecord协会
我有两个类:
Property -> has_one :contract
Contract -> belongs_to :property
在我的合同类,我不得不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
这是我卡住,我有合同属性的哈希值,和我想要做的是一样的东西:
if property.contract.nil?
# create a new one and populate it with attributes
else
# use the existing one and update it with attributes
我知道我会怎么做呢如果它是原始SQL,但我无法绕过ActiveRecord的方法。
任何提示通过这个路障将非常感激。
在此先感谢。
感谢的是,作品完美。 – 2012-01-16 00:03:23
也许使用空白? – Dan 2017-12-20 08:27:42