0

I have...

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

When I log into rails console, all seems OK initially...

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

...but then it turns strange...

> @standard.reports
 => [] 

Isn't it meant to be:

> @standard.reports
 => [#<Report id: 2, name: "foo", created_at: "2013-02-21 03:10:06", updated_at: "2013-02-21 03:10:06">] 

Why isn't it? How do I fix it?

steven_noble
  • 4,133
  • 10
  • 44
  • 77

1 Answers1

0

you are running @report.standards.build :name => "bar" which only builds the record and not create it in the database. if you change build to create, you should be able to see the associations.

jvnill
  • 29,479
  • 4
  • 83
  • 86