Inherited associations causing me pain

Brand extends Element. Element has two associations, to a creator and an
updater. Element exists to hold a selection of logic which is common
among many models in my site.

b = Brand.new
=> #<Brand:0x8f3cd40 @attributes={“visible”=>1, “name”=>nil,
“updated_at”=>nil, “creator_id”=>nil, “lock_version”=>0,
“type”=>“Brand”, “version”=>0, “updater_id”=>nil, “description”=>nil,
“created_at”=>nil}, @new_record=true>

b.attributes
=> {“visible”=>1, “name”=>nil, “updated_at”=>nil, “creator_id”=>nil,
“type”=>“Brand”, “lock_version”=>0, “version”=>0, “updater_id”=>nil,
“description”=>nil, “created_at”=>nil}

creator_id, updater_id nil as you would expect.

b.creator_id = 1
=> 1

b.attributes
=> {“visible”=>1, “name”=>nil, “updated_at”=>nil, “creator_id”=>1,
“type”=>“Brand”, “lock_version”=>0, “version”=>0, “updater_id”=>nil,
“description”=>nil, “created_at”=>nil}

I’ve made this attr_accessible and modifying them by hand works. As
shown here.

b.updater = Editor.find_by_id 1
=> #<Editor:0x8eab4bc @attributes={“name”=>“Administrator”,
“visible”=>“1”, “updated_at”=>“2006-11-03 15:05:57”, “level”=>“Editor”,
“lock_version”=>“3”, “id”=>“1”, “version”=>“0”,
“password”=>“207f881aa4c15f3149ad0be8c25f16dd59a0a44f”,
“email”=>“[email protected]”, “created_at”=>“2006-11-03 12:54:31”}>

Try using an assignment of an Editor instance now. I’d expect updater_id
to be set to the id of the editor (1).

b.attributes
=> {“visible”=>1, “name”=>nil, “updated_at”=>nil, “creator_id”=>1,
“type”=>“Brand”, “lock_version”=>0, “version”=>0, “updater_id”=>nil,
“description”=>nil, “created_at”=>nil}

But, it isn’t. If I try and save the brand, I get an exception.

Is this right? I tried working around this by:

  1. Adding ‘def creator_id’ which returns self.creator.id. This works but
    it seems that calling save reads from the attributes rather than calling
    this method, which isn’t so suprising I guess.

  2. Overriding creator=(obj) and updater=(obj) to set the attribute.
    Except they never get called. Presumeably rails is overriding them after
    me, or something.

Where am I going wrong? I’m quite happy to post the models, but they are
very ordinary I don’t think that is the problem.

Thanks,
Dominic

Dominic M. wrote:

But, it isn’t. If I try and save the brand, I get an exception.

Is this right? I tried working around this by:

  1. Adding ‘def creator_id’ which returns self.creator.id. This works but
    it seems that calling save reads from the attributes rather than calling
    this method, which isn’t so suprising I guess.

  2. Overriding creator=(obj) and updater=(obj) to set the attribute.
    Except they never get called. Presumeably rails is overriding them after
    me, or something.

Where am I going wrong? I’m quite happy to post the models, but they are
very ordinary I don’t think that is the problem.

Thanks,
Dominic

If the parent class has no table for itself, and is solely meant to be
inherited make sure you have

class Element
self.abstract_class = true
end

or else all kinds of bizarre stuff can happen.

Alex W. wrote:

Dominic M. wrote:

But, it isn’t. If I try and save the brand, I get an exception.

Is this right? I tried working around this by:

  1. Adding ‘def creator_id’ which returns self.creator.id. This works but
    it seems that calling save reads from the attributes rather than calling
    this method, which isn’t so suprising I guess.

  2. Overriding creator=(obj) and updater=(obj) to set the attribute.
    Except they never get called. Presumeably rails is overriding them after
    me, or something.

Where am I going wrong? I’m quite happy to post the models, but they are
very ordinary I don’t think that is the problem.

Thanks,
Dominic

If the parent class has no table for itself, and is solely meant to be
inherited make sure you have

class Element
self.abstract_class = true
end

or else all kinds of bizarre stuff can happen.

Thanks Wayne. I’ve tried that but it hasn’t changed anything.
Useful tip though, I hadn’t heard of that setting before.

Dominic

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs