Hi guys,
I’m having a problem with a belongs_to relationship.
The thing is:
class Banner < ActiveRecord::Base
belongs_to :campaign
…
end
class Campaign < ActiveRecord::Base
has_one :banner, :dependent => :destroy
accepts_nested_attributes_for :banner
…
end
If I do:
campaign.update_attribute(:banner_attributes, {:content => “Changed”})
campaign.banner.id is incremented by one, asi if a new instance is
being created.
Also, if I do:
campaign.update_attributes({:banner_attributes, {:content =>
“Changed”}})
campaign.banner.id is nil.
Any ideas?
This is my rspec.
it “should not change banner id when banner updated” do
Advertiser = mock
advertiser = mock
Advertiser.stub!(:find).and_return(advertiser)
advertiser.stub!(:id).and_return(1)
advertiser.stub!(:new_record?).and_return(false)
advertiser.stub!(:destroyed?).and_return(false)
banner = Banner.create(:content => "Some content", :deep_link =>
“http://test.com”)
banner.should be_valid
campaign = Campaign.create(:advertiser_id => advertiser.id,
:banner_id => banner.id, :website_code => “test”)
campaign.banner = banner
campaign.banner.id.should == banner.id
campaign.update_attribute(:banner_attributes, {:content =>
“Changed”})
campaign.banner.content.should == "Changed"
campaign.banner.id.should == banner.id
end
This is the output for the update_attribute case:
[[email protected] app_core]$ rspec spec/campaign_spec.rb -d
…F
Failures:
- Campaign should not change banner id when banner updated
Failure/Error: campaign.banner.id.should == banner.id
expected: 36,
got: 37 (using ==)./spec/campaign_spec.rb:93
Finished in 0.23597 seconds
7 examples, 1 failure
And this is the output for the update_attributes case:
[[email protected] app_core]$ rspec spec/campaign_spec.rb -d
…F
Failures:
- Campaign should not change banner id when banner updated
Failure/Error: campaign.banner.id.should == banner.id
expected: 38,
got: nil (using ==)./spec/campaign_spec.rb:93
Finished in 0.20556 seconds
7 examples, 1 failure
Any thoughts?
Thanks a lot in advance!
–
Leonardo M…
There’s no place like ~