【发布时间】:2012-01-13 00:03:15
【问题描述】:
我一直在努力解决 ActiveRecord 关联,但我遇到了一些难题,无论我查看了多少 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 方法。
我们将不胜感激任何通过此路障的提示。
提前致谢。
【问题讨论】:
标签: ruby-on-rails activerecord