Skip to content

Commit

Permalink
allow setting belongs_to id to nil, even if nil is whiny
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Bullock authored and notahat committed Nov 1, 2009
1 parent abddd6b commit a98739d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/machinist/active_record.rb
Expand Up @@ -32,7 +32,7 @@ def self.assigned_attributes_without_associations(lathe)
attributes = {}
lathe.assigned_attributes.each_pair do |attribute, value|
association = lathe.object.class.reflect_on_association(attribute)
if association && association.macro == :belongs_to
if association && association.macro == :belongs_to && !value.nil?
attributes[association.primary_key_name.to_sym] = value.id
else
attributes[attribute] = value
Expand Down
11 changes: 11 additions & 0 deletions spec/active_record_spec.rb
@@ -1,5 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
require 'machinist/active_record'
require 'active_support/whiny_nil'

module MachinistActiveRecordSpecs

Expand Down Expand Up @@ -131,6 +132,16 @@ class Comment < ActiveRecord::Base
comment[:post].should be_nil
comment[:post_id].should_not be_nil
end

describe "on a belongs_to association" do
it "should allow explicitly setting the association to nil" do
Comment.blueprint { post }
Comment.blueprint(:no_post) { post { nil } }
lambda {
@comment = Comment.plan(:no_post)
}.should_not raise_error
end
end

describe "on a has_many association" do
before do
Expand Down

0 comments on commit a98739d

Please sign in to comment.