rails belongs_to外键为null

时间:2013-05-21 06:54:20

标签: ruby-on-rails model foreign-keys associations belongs-to

我有以下型号

class User    
has_many :projects, :through => :bids
has_many :bids, :dependent => :destroy
end

class Project
attr_accessible :name, :user_id

has_many :users, :through => :bids
has_many :bids, :dependent => :destroy

belongs_to :projectmanager, :class_name => "User", :foreign_key => "user_id"

end

class Bid
attr_accessible :project_id, user_id
belongs_to :user
belongs_to :project
end

正如您所看到的,我的项目类同时具有* has_many *和* belongs_to *到同一模型(用户

项目控制器我有

def new
@project = Project.new
@project.gencontr = current_user

其中 current_user 是Devise当前登录用户。

保存项目时,项目表中的 user_id 列始终为空。你能告诉我在这个问题上哪里出错......谢谢

1 个答案:

答案 0 :(得分:1)

关于创建动作

def create
  @project = Project.new(prams[:project])
  @project.projectmanager = current_user
  .......
end