I have two models
class User < ActiveRecord::Base
has_many :requests, :foreign_key => :recipi开发者_如何学编程ent_id
.
.
class Request < ActiveRecord::Base
belongs_to :user
The schema for Request is User, FK, recipient_id (that equals the user_id)
Inserting is working great, for activerecord is not
This is always nil
<%= request.user.inspect %>
Where this is not:
<%= request.inspect %>
#<Request id: 37, recipient_id: 7, created_at: "2010-11-12 23:28:04", updated_at: "2010-11-12 23:28:04">
Ideas?
I do not think the request model knows the correct foreign_key, it may be looking for user_id in it's table, try the following.
belongs_to :user, :foreign_key => :recipient_id
I believe it should be:
belongs_to :user, :class_name => 'User'
精彩评论