This project is archived and is in readonly mode.

#6324 ✓invalid
Rob Westgeest

Rails 3.0.3 has many#create does not create a record

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

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.

Comments and changes to this ticket

  • Matt Fawcett

    Matt Fawcett January 22nd, 2011 @ 05:59 PM

    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.

  • Rob Westgeest

    Rob Westgeest January 22nd, 2011 @ 08:20 PM

    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.

  • Rohit Arondekar

    Rohit Arondekar January 27th, 2011 @ 06:18 AM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages