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.

belongs_to relationship creation should fail if foreign key does not exist.

#5451

ItemA belongs_to ItemB
item_b_id is not in table ItemAs.

ItemA.new.create_item_b -> does NOT raise error

A new ItemB is saved to db.

ItemA.first.item_b == nil

Env:
ree 1.8.7 2010.02
rails 3.0.0.rc, 3.0.0.rc2

This test identifies the issue. It can be placed in AR test/cases/associations/belongs_to_associations_test.rb


  class ItemA < ActiveRecord::Base
    belongs_to :item_b
  end
  class ItemB < ActiveRecord::Base
  end

  def test_creating_the_belonging_object_when_foreign_key_does_not_exist
    ActiveRecord::Base.connection.create_table :item_as, :force => true do |t|
      t.string :name
    end

    ActiveRecord::Base.connection.create_table :item_bs, :force => true do |t|
      t.string :name
    end
    
    item_a = ItemA.create!("name" => "Apple")
    item_b = item_a.create_item_b("name" => "(fan)Boy")
    assert_equal item_b, item_a.item_b
    item_a.save!
    item_a = ItemA.last
    assert_equal item_b, item_a.item_b
  end

Reported by Matt Scilipoti · August 25th, 2010 @ 02:40 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Matt Scilipoti
    Matt Scilipoti

    This test identifies the issue but, it may be better to use:

    item_a = ItemA.create!("name" => "Apple")
    assert_raise <insert appropriate error here> do
      item_b = item_a.create_item_b("name" => "(fan)Boy")
    end
    

    August 25th, 2010 @ 02:15 PM

  2. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Tag changed from rails 3.0.rc, rails 3.rc2 to rails 30rc, rails 3rc2

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:33 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:33 PM