Forum: Ruby on Rails polymorphic associations ok in read but not in create...

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Romain E. (Guest)
on 2007-01-05 18:33
 Hi, I am trying to use polymorphic associations to solve the following
I want a general class 'Employee' which content can come from 2 other
The code is:

class Employee < ActiveRecord::Base
# :assignable is the interface we expose to mask of various employee
belongs_to :assignable, :polymorphic => true

class User < ActiveRecord::Base
# polymorphic association
has_many :employees, :as => :assignable #here I am not sure if I should
put has_many or has_one ?

class Secretary < ActiveRecord::Base
# polymorphic association
has_many :employees, :as => :assignable

In Read mode, everything is fine :
Employee.find(:first).class # => Employee.
Employee.find(:first).assignable.class # => User or Secretary ,
depending on the data.

Now, I am writing my Unit Tests and I want to see if when I create a
User, the Employee entry is also created or not.
Apparently, does not create a record in Employee, which I mean
I have to code this.

Am I doing something wrong in my code or is it simply that it is not
[yet] implemented in Rails ?

thanks in advance.

This topic is locked and can not be replied to.