0

I have a User model who has one Profession (Designer, Model, Photograph, ...). Each profession has specific attributes, so each profession has its own table.

Now in my User I would like to say that a User has_one :profession, where the profession is Photograph, Designer, or whatever else.

My first idea was to use the Profession model as a STI but if I do this I cannot have a table for each real profession...

How would you design this?

Thanks, Greg

4

1 回答 1

2

我会将has_one关联翻转为 abelongs_to并使用多态关联。模型代码将是:

class User < ActiveRecord::Base
  belongs_to :profession, :polymorphic => true
end


# similar code for other professions
class Designer < ActiveRecord::Base
  has_one :user, :as => :profession
end

迁移将如下所示:

change_table :users do |t|
  t.references :profession, :polymorphic => true
end
于 2012-12-10T19:25:47.593 回答