铁轨模型关系

时间:2013-01-16 11:24:49

标签: ruby-on-rails relation

我有用户模型和反馈模型,用户模型有user_type属性。 反馈模型也有user_id属性。我想将user_type列添加到反馈模型中,与用户模型中的相同。例如,在迁移后它应该是那样的

User
id:123
user_type: "customer"

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

1 个答案:

答案 0 :(得分:0)

为什么你也将user_type放在反馈表中。您可以使用rails association从user_id检索它。

class User < ActiveRecord::Base
  has_many :feedback
end 

class Feedback < ActiveRecord::Base
  belongs_to :user
end 

要从反馈对象中检索user_type,请使用@feedback.user.user_type

相关问题