2013-01-16 77 views
0

我有用户模型和反馈模型,并且用户模型具有user_type属性。 反馈模式也有user_id说明attribute.I想迁移应该是这样的导轨模型关系

User 
id:123 
user_type: "customer" 

Feedback 
id:56 
user_id:123 
user_type : "customer" 

回答

0

为什么你把USER_TYPE反馈表太后USER_TYPE列添加到反馈模型的相同用户model.For实例。您可以使用rails关联从user_id中检索它。

class User < ActiveRecord::Base 
    has_many :feedback 
end 

class Feedback < ActiveRecord::Base 
    belongs_to :user 
end 

从反馈对象retreive USER_TYPE使用@feedback.user.user_type

+0

你的意思的has_many:反馈 – shweta

+0

取决于需求,如果用户有很多的反馈,然后使用的has_many:在用户模式的反馈。 –

+0

由于'users'表中没有'feedback_id'字段,它必须是'has_many:feedbacks'或'has_one:feedback',而不是'belongs_to:feedback'。 – PinnyM