我一直在試圖讓我的腦袋圍繞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