Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rails 3.0.3 has many#create does not create a record

#6324

Given a these models

class OrganizingCity < ActiveRecord::Base
  has_many :tables
end
class Table < ActiveRecord::Base
  belongs_to :organizing_city
end

and the specs (using FixtureReplacement)

describe OrganizingCity do
  describe "create a table" do
    it "through tables#create add a table to the has_many association" do
      organizing_city = create_organizing_city
      organizing_city.tables.create new_table.attributes
      organizing_city.reload
      organizing_city.tables.size.should == 1
    end
    it "through creating a new table adds a table to the has many association" do
      organizing_city = create_organizing_city
      Table.create organizing_city.tables.create new_table(:organizing_city => organizing_city).attributes
      organizing_city.reload
      organizing_city.tables.size.should == 1
    end
  end
end

then the first one fails and the second one passes.

Reported by Rob Westgeest · January 22nd, 2011 @ 10:55 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Matt Fawcett
    Matt Fawcett

    This won't be a Rails problem. Its likely that new_table.attributes is returning a hash which has a key :organizing_city_id with a value of nil, causing the table to get saved without the foreign key.

    January 22nd, 2011 @ 05:59 PM

  2. Rob Westgeest
    Rob Westgeest

    You're right!. changing the first spec to:

        it "through tables#create add a table to the has_many association" do
          organizing_city = create_organizing_city
          table_attributes = new_table.attributes
          table_attributes.delete "organizing_city_id"
          organizing_city.tables.create table_attributes
          organizing_city.reload
          organizing_city.tables.size.should == 1
        end
    

    makes it pass.

    Sorry to have poluted this list with a worthless ticket. This can be closed.

    January 22nd, 2011 @ 08:20 PM

  3. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to invalid
    • Importance changed from to Low

    January 27th, 2011 @ 06:18 AM