Tim*_*aas 1 ruby-on-rails associations
我一直在寻找一段时间,但谷歌并没有真正帮助我.ArgumentError Unknown key(s): client_id出现在ProjectsController中:
# projects_controller.rb
class Management::ProjectsController < Management::ManagementController
def index
@projects = Project.find( :client_id => current_user.client )
end
end
Run Code Online (Sandbox Code Playgroud)
这是项目模型:
# project.rb
class Project < ActiveRecord::Base
belongs_to :client
end
Run Code Online (Sandbox Code Playgroud)
这是客户端模型:
# client.rb
class Client < ActiveRecord::Base
has_many :projects
end
Run Code Online (Sandbox Code Playgroud)
最后,迁移:
# 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
Run Code Online (Sandbox Code Playgroud)
应该可以吧?
看不到我在这里失踪的东西..
有人有建议吗?
谢谢!
使用
@projects = Project.where( :client_id => current_user.client.id)
Run Code Online (Sandbox Code Playgroud)
要么
@projects = Project.find_by_client_id(current_user.client.id)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4248 次 |
| 最近记录: |