This project is archived and is in readonly mode.

#6647 new

ActiveRecord::Associations::ClassMethods build is kind of misleading with unsaved parent objects

Reported by Anonymass | March 30th, 2011 @ 04:01 AM

Hi there: returns unexpected behavior when returning a reference to a unsaved parent object

class Recipe < ActiveRecord::Base
  has_many :ingredients
class Ingredient < ActiveRecord::Base
  belongs_to :recipe
>> r =
=> #<Recipe id: nil, title: nil, created_at: nil, updated_at: nil>
=> #<Ingredient id: nil, name: nil, recipe_id: nil, created_at: nil, updated_at: nil>
>> r.ingredients.first.recipe
=> nil

Why nil?

=> true
>> r.ingredients.first.recipe
=> #<Recipe id: 3, title: nil, created_at: "2011-03-30 02:49:58", updated_at: "2011-03-30 02:49:58">

One would assume a reference to the parent object would be returned and not nil even though the parent object has not been saved

( It would totally make sense if recipe_id would return nil, but recipe should be the unsaved parent object )

This picture sums up how I'm feeling right now:


No comments found

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=""></a>

People watching this ticket