hzamani / acts_as_relation

Multi table Inheritance for rails
http://hzamani.github.com/acts_as_relation/
MIT License
180 stars 58 forks source link

Association Type Mismatch error #41

Closed kot-behemoth closed 10 years ago

kot-behemoth commented 11 years ago

I'm having a strange issue with acts_as_relation. I have my (simplified) models structure as follows:

class User < ActiveRecord::Base
  has_one :occupation
end

class Occupation < ActiveRecord::Base
  acts_as_superclass

  belongs_to :user
  validates :user_id, presence: true
end

class Worker < ActiveRecord::Base
  acts_as :occupation, as: :occupation
end

In my migration, I used

  create_table "occupations", force: true do |t|
    t.integer "occupation_id"
    t.string  "occupation_type"
    t.integer "user_id"
  end

With this hierarchy (very similar to bigardone's one), I was expecting to be able to do the following simple assignment

user.occupation = Worker.new

however I'm getting this error:

ActiveRecord::AssociationTypeMismatch: Occupation expected, got Worker

I realised that you don't actually say this is possible anywhere in the docs, but I thought it would be. Currently, I'm working around this as follows:

user.occupation = Worker.new.occupation

which feels a bit like a hack.

I understand this is a very basic question, but I am relatively new to Rails, so I am wondering if I'm missing something?

doncote commented 11 years ago

I'm curious about this one as well. I would expect polymorphism but maybe that's not the case?

hzamani commented 10 years ago

fixed