This is my Feedback model:
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:
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:
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.
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.
How do I do that?
Thanks.
P.S. Using Rails 3.1…if that has any bearing.
P.P.S. In my User model the has_many :feedback line looks weird, because I know it should be plural. But has_many :feedbacks looks even worse 😐 What should I do for that too?
P.P.P.S. 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? That looks weird and doesn’t seem to fit the description in the api documentation of the foreign_key attribute.
Your associations should be defined like this:
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.