开发者

Rails - Help with a User?Request model relationship

开发者 https://www.devze.com 2023-01-24 16:54 出处:网络
I have two models class User < ActiveRecord::Base has_many :requests, :foreign_key => :recipi开发者_如何学编程ent_id

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

精彩评论

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