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

lighthouse-import opened this Issue May 16, 2011 · 0 comments


None yet
1 participant

Imported from Lighthouse. Original ticket at:
Created by Anonymass - 2011-03-30 03:01:36 UTC

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:


jake3030 pushed a commit to jake3030/rails that referenced this issue Jun 28, 2011

Added grouped_options_for_select helper method for wrapping option ta…
…gs in optgroups. [#977 state:resolved]

Signed-off-by: Pratik Naik <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment