2013-01-16 157 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