0

我有一些小型 ROR“主要是静态”类型的网站,并且正在研究一些我没有太多经验的更深层次的东西......

大图命名法。帐户有许多用户和项目。用户有很多项目。用户向项目中添加了许多文件和注释....

最后,我需要生成一个视图,用户可以在其中查看他们的项目、文件和注释。在类似于以下手动创建的“概念证明”的列表或表格中:

Project1
    Notes
        note1
        note2
        note3
    Files
        file1
        file2
        file2
    Users
        user1
        user2
Project2
    Notes
        note1
        note2
        note3
    Files
        file1
        file2
        file2
    Users
        user1
        user2

上面的列表将使用某种嵌入式 ruby​​ for 循环生成,但在我开始之前,我需要确保我有正确的模型关联和调用。

我试图从我的所有表中都没有外键,但我对多模型最佳实践感到非常困惑。没有外键我想我需要一个连接表,它可以是使用“model1_model2”命名约定的模型?和“:通过”模型关系?

我现在的模型(这已经发生了很大变化)是:

帐户:

class Account < ActiveRecord::Base
has_many :projects
has_many :users

结尾

用户:

class User < ActiveRecord::Base
has_one :account
has_many :projects, :through => :accounts
has_many :dataFiles, :through => :projects
has_many :notes, :through => :projects

结尾

项目:

class Project < ActiveRecord::Base
belongs_to :account
has_many :users
has_many :datafiles
has_many :notes

结尾

数据文件:

class DataFile < ActiveRecord::Base
belongs_to :projects
belongs_to :users

结尾

笔记:

class Note < ActiveRecord::Base
belongs_to :project
belongs_to :users

结尾

如您所见;我在这里很困惑!我做了很多教程并读了一本书;这是我的第一个现实世界应用程序,它不仅仅是主要的静态页面......

似乎有很多方法可以做到这一点。我想我正在寻找一些关于我应该使用哪些模型以及如何连接它们的专家指导。

非常感谢您提供的任何方向或建议。谢谢!

4

2 回答 2

1

如果我理解正确

class Accounts < ActiveRecord::Base
  # these two associations say an Account can have many users. 
  # It's also assuming users can be associated with multiple accounts. If that's false
  # i'd recommend putting the account_id on the user and simply removing this many-to-many table
  has_many :account_users
  has_many :users, :through => :account_users

  # accounts can be mapped to many projects and projects can be mapped to many accounts
  # if a project only belongs to one account, drop the accounts_projects many-to-many table
  # and just put the account_id on the project.
  has_many :account_projects
  has_many :projects, :through => :account_projects
end

# account_user table will have `id`, `account_id`, `user_id` and anything else you need
class AccountUser < ActiveRecord::Base
  belongs_to :account
  belongs_to :user
end   

class User < ActiveRecord::Base
  has_many :projects
  has_many :files
end

# account_projects table will have `id`, `account_id`, `project_id` and anything else you need
class AccountProject < ActiveRecord::Base
  belongs_to :account
  belongs_to :project
end

class Project < ActiveRecord::Base
  has_many :data_files
  has_many :notes

  has_many :project_users
  has_many :users
end

# project_users table will have `id`, `project_id`, `user_id` and anything else you need
class ProjectUser < ActiveRecord::Base
  belongs_to :project
  belongs_to :user
end

# data_files table will have `id`, `project_id`, `user_id` and anything else you need
class DataFile < ActiveRecord::Base
  belongs_to :project
  belongs_to :user
end

# notes table will have `id`, `project_id`, `user_id` and anything else you need
class Note < ActiveRecord::Base
  belongs_to :project
  belongs_to :user
end

这是否有助于解释这些关联将如何在 Rails 中工作?

注意AccountUser、AccountProject 和 ProjectUser 表都用于我理解您需要的多对多关联。

如果您要将任何其他属性与关联的映射关联(例如与帐户和用户相关的东西),则通过关联非常有用。

如果您只需要一个简单的多对多关系而不需要自定义属性,则可以简单地使用该has_and_belongs_to_many方法,尽管我通常会预先选择 :through 选项。

于 2011-08-31T17:15:26.693 回答
0

没有回车评论字段让我恼火......

用户只能属于一个帐户(管理员除外),所以我想我会采纳您的建议并删除多对多表并在用户表中使用 account_id 字段。对于accounts_projects 多对多...

听起来,将所有这些模型关联起来的最佳方式是将“belongs_to”和“has_many”与存储在相应表中的外键关联起来。我感谢您提供的教育以及对所有这些模型相关的真实世界洞察力。谢谢!希望我在设置这一切时不会遇到任何问题。

谢谢!

于 2011-09-02T13:39:12.340 回答