链接范围时,DataMapper关联会丢失

时间:2019-01-15 16:18:35

标签: datamapper ruby-datamapper

我正在使用DataMapper的链接范围。当仅调用一个作用域时,关联将正确加载,但是当添加第二个作用域时,该关联将为null。我的belongs_to关系被允许为null,因为它是新添加到Order模型中的。该架构会在初始化时更新,然后添加not null约束。

class Order
  include DataMapper:Resource

  belongs_to :customer, :required => false
end

# do the migration which creates a new customers table and 
# then updates the orders table with the proper customer_id

DataMapper.repository(:default).adapter.select("ALTER TABLE orders ALTER COLUMN customer_id NOT NULL")

# do a lookup

order = Order.get(1) # order.customer_id is null and so is order.customer

orders = Order.all # each order in array has customer not null

orders = orders.all(status: 'HOLD') # each order in array has a null customer

0 个答案:

没有答案