我正在尝试在我的 RoR 模型中建立一些高级关系。
到目前为止,我所做的是项目和任务——项目有任务,用户有项目。
我现在希望“注册”到项目的用户现在可以“注册”到该项目中的任务。
我目前的课程简介:
class AdminUser < ActiveRecord::Base
has_and_belongs_to_many :projects
has_and_belongs_to_many :tasks
has_many :admin_users_projects
[...]
class AdminUsersProject < ActiveRecord::Base
belongs_to :admin_user
has_and_belongs_to_many :project
has_many :tasks
[...]
class Project < ActiveRecord::Base
has_many :tasks
has_and_belongs_to_many :admin_users
has_and_belongs_to_many :admin_users_projects
[...]
class Task < ActiveRecord::Base
belongs_to :project
has_and_belongs_to_many :admin_users
belongs_to :admin_users_project
[...]
架构如下所示:
create_table "admin_users", :force => true do |t|
t.string "first_name", :limit => 25
t.string "last_name", :limit => 50
t.string "email", :limit => 100, :default => "", :null => false
t.string "hashed_password", :limit => 40
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "username", :limit => 25
t.string "salt", :limit => 40
end
add_index "admin_users", ["username"], :name => "index_admin_users_on_username"
create_table "admin_users_projects", :force => true do |t|
t.integer "admin_user_id"
t.integer "project_id"
end
add_index "admin_users_projects", ["admin_user_id", "project_id"], :name => "index_admin_users_projects_on_admin_user_id_and_project_id"
create_table "projects", :force => true do |t|
t.string "name"
t.integer "position"
t.boolean "visible", :default => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "tasks", :force => true do |t|
t.integer "project_id"
t.string "permalink"
t.integer "position"
t.boolean "visible"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name"
t.integer "admin_user_id"
t.integer "progress"
end
add_index "tasks", ["permalink"], :name => "index_tasks_on_permalink"
add_index "tasks", ["project_id"], :name => "index_tasks_on_project_id"
end
我有一个显示项目的视图,然后是他们的任务子集的视图我还有一个显示用户项目的视图,但现在我希望它显示用户的任务子集。
我对“admin_user_projects”的简要看法如下:
<% @admin_users_projects.each do |admin_users_projects| %>
<tr>
<td><%= admin_users_projects.admin_user_id %></td>
<td><%= admin_users_projects.admin_user.username %></td>
<td><%= admin_users_projects.project_id %></td>
<td><%= admin_users_projects.project.name %></td>
</tr>
<% end %>
这显示了用户 ID、用户名、项目 ID 和项目名称。我现在想添加一行,显示用户为该项目“注册”了多少任务。我努力了
<td><%= admin_users_projects.tasks.size %></td>
但是得到以下信息:
Mysql2::Error: Unknown column 'tasks.admin_users_project_id' in 'where clause': SELECT COUNT(*) FROM `tasks` WHERE `tasks`.`admin_users_project_id` = 1
有谁知道如何让这些关系有效运作?