0

私は持っています...

app / models / report.rb:

has_and_belongs_to :standards

app / models / standard.rb:

has_and_belongs_to :reports

db / 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"レコードを作成するだけで、データベースに作成しない実行中です。build を create に変更すると、関連付けが表示されるはずです。

于 2013-02-21T03:49:19.460 に答える