0

我有...

应用程序/模型/report.rb:

has_and_belongs_to :standards

应用程序/模型/标准.rb:

has_and_belongs_to :reports

分贝/schema.rb:

create_table "reports_standards", :id => false, :force => true do |t|
  t.integer "report_id"
  t.integer "standard_id"
end

当我登录到 Rails 控制台时,最初似乎一切正常......

> @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">] 

为什么不是?我如何解决它?

4

1 回答 1

0

您正在运行@report.standards.build :name => "bar"它只构建记录而不是在数据库中创建它。如果您将构建更改为创建,您应该能够看到关联。

于 2013-02-21T03:49:19.460 回答