0

I know active record require for User use user_id if you want to set up foreign key, but for project needed we must use three different fields for storing different users (responsible,executor,validation users)

So now it looks like

Task
-----------
name | date | until | initiation | validation | execution  

I think it should looks like

has_one :user,class => User,:as=> :initiation

or similar, but still cant find right way

4

2 回答 2

2

如果我理解正确,你有任务模型,它有三种类型的用户处理它。你想引用它们,对吧?

如果是这样,那么首先通过迁移将initialization_id、validation_id 和 execution_id 字段放入您的任务表中。然后在您的任务模型中尝试这个:

has_one :initiation, :class => "User", :foreign_key => :initiation_id
has_one :validation, :class => "User", :foreign_key => :validation_id
has_one :execution, :class => "User", :foreign_key => :execution_id

希望我理解你的正确。

于 2013-09-16T21:02:04.870 回答
1

我不完全确定您需要什么,但我认为您正在寻找多态关联单表继承(STI)

无论哪种方式,都有关于这个主题的 Railscast-episode(仅限专业版) - 你应该看看它是否是你正在寻找的。

于 2013-09-16T20:31:35.833 回答