简体   繁体   English

如何在模型上引用“ user_id”,但命名为“ poster_id”和“ receiver_id”两个不同的属性? -滑轨3

[英]How do I reference a 'user_id' on a model, but named as two different attributes 'poster_id' and 'receiver_id'? - Rails 3

This is my Feedback model: 这是我的Feedback模型:

class Feedback < ActiveRecord::Base
    belongs_to  :user
end
# == Schema Information
#
# Table name: feedbacks
#
#  id          :integer         not null, primary key
#  poster_id   :integer
#  receiver_id :integer
#  content     :string(255)
#  created_at  :datetime
#  updated_at  :datetime

This is my User model: 这是我的User模型:

class User < ActiveRecord::Base
    has_many    :feedback
end
# == Schema Information
#
# Table name: users
#
#  id         :integer         not null, primary key
#  email      :string(255)
#  f_name     :string(255)
#  l_name     :string(255)
#  username   :string(255)
#  role_id    :integer
#  picture    :string(255)
#  about_me   :string(255)
#  website    :string(255)
#  created_at :datetime
#  updated_at :datetime

What I would like to do is retrieve all the feedback for a particular user. 我想做的是检索特定用户的所有反馈。 But, at my console, when I do t = User.first and then t.feedback , I get the following error: 但是,在我的控制台上,当我先执行t = User.first然后t.feedback ,出现以下错误:

 Feedback Load (0.2ms)  SELECT "feedbacks".* FROM "feedbacks" WHERE "feedbacks"."user_id" = 1
SQLite3::SQLException: no such column: feedbacks.user_id: SELECT "feedbacks".* FROM "feedbacks"  WHERE "feedbacks"."user_id" = 1
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: feedbacks.user_id: SELECT "feedbacks".* FROM "feedbacks"  WHERE "feedbacks"."user_id" = 1

Now that is saying that I have no column user_id in my Feedbacks table, which makes sense - because I don't. 这就是说我的“反馈”表中没有列user_id ,这很有意义-因为我没有。

What I really want to happen is, I want to store the user_id of a user that posts feedback as the poster_id , and when someone receives feedback I want the user_id to be stored in the feedback object as receiver_id . 我真的希望发生的是,我希望存储user_id用户的该posts的反馈,因为poster_id ,当有人收到反馈我想user_id存储在反馈对象receiver_id

How do I do that? 我怎么做?

Thanks. 谢谢。

PS Using Rails 3.1...if that has any bearing. PS Using Rails 3.1 ...(如果有任何轴承)。

PPS In my User model the has_many :feedback line looks weird, because I know it should be plural. PPS在我的User模型中, has_many :feedback行看起来很奇怪,因为我知道它应该是复数形式。 But has_many :feedbacks looks even worse :| 但是has_many :feedbacks看起来更糟:| What should I do for that too? 我也应该怎么做?

PPPS I looked at the foreign_key attribute of associations, but wasn't sure how to get it to work in this situation - would the association be belongs_to :poster_id, :foreign_key => user_id ? PPPS我查看了关联的foreign_key属性,但不确定在这种情况下如何使它工作-关联是否为belongs_to :poster_id, :foreign_key => user_id That looks weird and doesn't seem to fit the description in the api documentation of the foreign_key attribute. 这看起来很奇怪,而且似乎不符合foreign_key属性的api文档中的描述。

Your associations should be defined like this: 您的关联应这样定义:

class Feedback < ActiveRecord::Base
    belongs_to :poster, :class_name => 'User'
    belongs_to :receiver, :class_name => 'User'
end

class User < ActiveRecord::Base
    has_many :feedbacks_as_poster, :foreign_key => :poster_id, :class_name => 'Feedback'
    has_many :feedbacks_as_receiver, :foreign_key => :receiver_id, :class_name => 'Feedback'
end

If you are not sure about what the association name means or you don't like the name, you should possibly think about a little bit more about your model, maybe the naming doesn't reflect what you're trying to model with your objects. 如果您不确定关联名称的含义,或者您不喜欢该名称,则可能应该多考虑一下模型,也许命名不能反映您要使用对象建模的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM