开发者

Rails3, Unknown key(s): client_id, on belongs_to association

I've been searching for a while now, but google isn't really helping me. The ArgumentError Unknown key(s): client_id appears in the ProjectsController:

# projects_controller.rb

class Management::ProjectsController < Management::ManagementController
  def index
    @projects = Project.find( :client_id => current_user.client )
  end
end

This is the project model:

# project.rb

class Project < 开发者_C百科ActiveRecord::Base
  belongs_to :client
end

This is the client model:

# client.rb

class Client < ActiveRecord::Base
  has_many :projects
end

And finally, the migration:

# 20110404155917_create_projects.rb

class CreateProjects < ActiveRecord::Migration
  def self.up
    create_table :projects do |t|
      t.string :name
      t.datetime :date
      t.text :description
      t.integer :client_id
      t.timestamps
    end
  end

  def self.down
    drop_table :projects
  end
end

Should be possible, right?

Can't see what I'm missing here..

Anyone got a suggestion?

Thanks!


Use

@projects = Project.where( :client_id => current_user.client.id)

or

@projects = Project.find_by_client_id(current_user.client.id)


or you could do

@projects = current_user.client.projects

Little bit cleaner perhaps?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜