开发者

Rails - Help with a User?Request model relationship

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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜