0

我有这个代码:

bar = Bar.new
bar.build_foo

在 foo 模型中,我有:

validates :baz, :presence => true, :if lambda { self.bar.terminal.is_active == true }

在 lambda self.bar is nil 我明白这一点,但是有没有办法在 lambda 块中获取关联栏?

4

1 回答 1

1

您需要指定反向关联:

class Bar < ActiveRecord::Base
  has_one :foo, inverse_of: :bar
end

class Foo < ActiveRecord::Base
  belongs_to :bar, inverse_of: :foo
end
于 2014-05-08T13:57:33.167 回答