0
我有...为什么has_and_belongs_to_many生成的方法不是双向的?
应用程序/模型/ report.rb:
has_and_belongs_to :standards
应用程序/模型/ standard.rb:
has_and_belongs_to :reports
DB /架构。 rb:
create_table "reports_standards", :id => false, :force => true do |t|
t.integer "report_id"
t.integer "standard_id"
end
当我登录到控制台的轨道,一切似乎都OK最初...
> @report = Report.create :name => "foo"
=> #<Report id: 2, name: "foo", created_at: "2013-02-21 03:10:06", updated_at: "2013-02-21 03:10:06">
> @standard = @report.standards.build :name => "bar"
=> #<Standard id: nil, name: "bar", created_at: nil, updated_at: nil>
> @report.standards
=> [#<Standard id: nil, name: "bar", created_at: nil, updated_at: nil>]
...但后来事实证明奇怪...
> @standard.reports
=> []
是不是意思是:
> @standard.reports
=> [#<Report id: 2, name: "foo", created_at: "2013-02-21 03:10:06", updated_at: "2013-02-21 03:10:06">]
为什么不是这样?我如何解决它?