Rails has_one with class name and foreign key

has_one :requester, :class_name => "User", :foreign_key => "requester_id"

This line (from the code that you posted) indicates that the requester is a User, and the table users should contain a column requester_id that is the foreign key toward friend_requests records. The rails error message states that the column requester_id does not exists (you have to create it via a migration).

In this case, use

rails generate migration AddRequesterIdToUsers requester_id:integer

It will generate the migration:

class AddRequesterIdToUsers < ActiveRecord::Migration
  def change
    add_column :users, :requester_id, :integer
  end
end  

And run them migration with rake db:migrate.

Look at the Rails Relation Guide for more information on differences between has_one and belongs_to, and how to use them.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)