Search code examples
ruby-on-railsmodelforeign-keysassociationsbelongs-to

rails belongs_to foreign key is null


i have the following models

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

As you can see, my Project class has both *has_many* and *belongs_to* to the same model (User)

In Project controller new I have

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

where current_user is Devise current logged in user.

When I save the project, the column user_id in the Projects table is always null. Can you show me where do I go wrong on this one...Thank you


Solution

  • on create action

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