Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 建立关联不应该完成反向关系吗?_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails 3 建立关联不应该完成反向关系吗?

Ruby on rails 3 建立关联不应该完成反向关系吗?,ruby-on-rails-3,activerecord,Ruby On Rails 3,Activerecord,我惊讶地发现,考虑到: class Foo has_many :bars attr_accessible :baz end class Bar belongs_to :foo, inverse_of: :foo end 然后这样做: new_bar = foo.bars.build params puts new_bar.foo.baz 发出对数据库的调用以获取foo。什么 您可以通过自己完成反向关系来防止: new_bar.foo = foo 我做错什么了吗?有什么好的理由认

我惊讶地发现,考虑到:

class Foo
  has_many :bars
  attr_accessible :baz
end
class Bar
  belongs_to :foo, inverse_of: :foo
end
然后这样做:

new_bar = foo.bars.build params
puts new_bar.foo.baz
发出对数据库的调用以获取foo。什么

您可以通过自己完成反向关系来防止:

new_bar.foo = foo
我做错什么了吗?有什么好的理由认为,了解关系双方的一切情况的build不应该强迫它呢