I am trying to make some advanced relationships in my RoR models.
S far what I have working is projects and tasks- Projects have tasks and users have projects.
I now want that a user who is 'signed up' to a project can now 'sign up' to tasks within that project.
My current classes in brief:
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
[...]
The Schema looks like this:
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
I have view that show projects and then a view of their subset of tasks I also have a view that shows a users projects but now I want it to show that users subset of tasks.
My view for 'admin_user_projects' in brief is as follows:
<% @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 %>
This shows the users id, username, project id and project name. I now want to add a row showing how many tasks a user is 'signed up' to for that project. I have tried
<td><%= admin_users_projects.tasks.size %></td>
But get the following:
Mysql2::Error: Unknown column 'tasks.admin_users_project_id' in 'where clause': SELECT COUNT(*) FROM `tasks` WHERE `tasks`.`admin_users_project_id` = 1
Does anyone have any ideas how to get these relationships working effectively?