gpt4 book ai didi

mysql - Ruby on Rails 中跨 4 个模型的关系

转载 作者:行者123 更新时间:2023-11-29 14:01:04 26 4
gpt4 key购买 nike

我正在尝试在我的 RoR 模型中建立一些高级关系。

到目前为止,我所做的工作是项目和任务 - 项目有任务,用户有项目。

我现在希望“注册”项目的用户现在可以“注册”该项目中的任务。

我目前的类(class)简介:

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

我有显示项目的 View ,然后是其任务子集的 View 我还有一个显示用户项目的 View ,但现在我希望它显示用户任务的子集。

我对“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

有人知道如何让这些关系有效运作吗?

最佳答案

我的建议是这样的:

class AdminUser < ActiveRecord::Base
has_and_belongs_to_many :projects
has_many :assigned_tasks
has_many :tasks, through: :assigned_tasks
end

class AssignedTask < ActiveRecord::Base
belongs_to :user
belongs_to :task

validate :user_assigned_to_project

private

def user_assigned_to_project
unless user.projects.include? task.project
errors.add(:project, "is not valid")
end
end
end

class Project < ActiveRecord::Base
has_and_belongs_to_many :admin_users
has_many :tasks
end

class Task < ActiveRecord::Base
has_many :assigned_tasks
has_many :users, through: :assigned_tasks
belongs_to :project
end


class GiantMigration < ActiveRecord::Migration
create_table :admin_users do |t|
# whatever
end
create_table :projects do |t|
# whatever
end
create_table :assigned_tasks do |t|
t.integer :admin_user_id, null: false
t.integer :task_id, null: false
end
create_table :tasks do |t|
# whatever
t.integer :project_id
end
create_table :admin_users_projects, id: false do |t|
t.integer :admin_user_id, null: false
t.integer :project_id, null: false
end
end

一些一般注意事项:

  • 您必须选择是要使用纯联接表 (HABTM) 还是联接模型 (has_many :through)。现在你正在混合两者。
  • 请勿在迁移中默认使用 force: true
  • 遵守 Rails 的约定:has_and_belongs_to_many :projects(您使用了单数项目)

关于mysql - Ruby on Rails 中跨 4 个模型的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15045799/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com