#599 ✓resolved
Marcin Kulik

update_attributes doesn't work for STI (inherited) models with belongs_to

Reported by Marcin Kulik | October 17th, 2008 @ 04:19 AM

Hi guys, I've found that update_attributes doesn't save changes to model which is inherited from another model (STI) AND which defines new belongs_to relation. I've prepared following spec to reproduce this:


require 'rubygems'
require 'spec'
require 'dm-core'
require 'dm-validations' # this breaks update_atrributes, even if I don't use any validations

DataMapper.setup(:default, 'sqlite3::memory:')

class User
  include DataMapper::Resource

  property :id, Serial
  property :name, String
  property :type, Discriminator
end

class Role
  include DataMapper::Resource

  property :id, Serial
  property :name, String
end

class Employee < User
  property :role_id, Integer

  belongs_to :role
end

DataMapper.auto_migrate!

describe User do
  it "should update name on freshly created user" do
    @user = User.create(:name => "jola")
    @user.new_record?.should be_false
    @user = User.get(@user.id)
    @user.update_attributes(:name => "misio").should be_true
    @user.name.should == "misio"
  end
end

describe Employee do
  before(:all) do
    @role = Role.create(:name => "a cat")
    @role.new_record?.should be_false
  end

  before(:each) do
    @employee = Employee.create(:name => "jola", :role => @role)
    @employee.new_record?.should be_false
  end

  it "should update name on freshly created employee" do
    @employee.update_attributes(:name => "misio").should be_true
    @employee.name.should == "misio"
  end

  it "should update name on loaded employee" do
    @employee = User.get(@employee.id)
    # @employee.role # <- uncommenting this fixes the problem
    @employee.update_attributes(:name => "misio").should be_true
    @employee.name.should == "misio"
  end
end

It appears that requiring 'dm-validations' breaks update_attributes (probably attributes=). I've found that touching @employee.role fixes that problem...

Thing to note is that:


@employee.update_attributes(:name => "misio")

in above spec returns true although the attribute isn't updated.

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Attachments

Pages