0

我有几个小ROR“大多是静态的”类网站和我工作的更深层次的东西一点点,我没有用太多的经验...的Ruby on Rails应用程序使用多个模型

大图片命名。 帐户有许多用户和项目。用户有很多项目。用户添加很多文件和笔记到项目中......

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

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

上面会使用某种for循环的嵌入红宝石来产生列表但在此之前,我到我需要确保我有适当的模型关联和调用。

我试图从我所有的表中没有外键,但我一直在与多模式最佳实践混淆。没有外键我想我需要一个连接表,它可以是一个使用“model1_model2”命名约定?和“:through”模型关系的模型?

我的模型现在(这已经改变了很多)有:

帐户:

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 

正如你所看到的;我很困惑!我已经完成了一系列教程并阅读了一本书;这是我的第一个真正的世界应用程序,不只是主要是静态页面... ...

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

任何方向或建议,你可以提供非常感谢。谢谢!

回答

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 

理解您这是否有助于解释如何协会将在轨工作?

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

如果您打算将任何其他属性与关联映射相关联(例如与帐户和用户有关的关系),则直通关联非常有用。

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

+0

这非常有帮助!感谢您的指导。 – twinturbotom

0

的没有回车注释字段刺激我......

用户只能属于一个帐户(除了管理员),所以我想我会接受你的建议,并删除多对多表,并使用一个ACCOUNT_ID用户表中的字段。 accounts_projects多对多...

听起来好像关联所有这些模型的最佳方式是“belongs_to”和“has_many”,外键存储在适当的表中。我很欣赏你所提供的教育以及关联所有这些模型的真实世界洞察力。谢谢!希望我不会遇到任何设置这一切的问题。

谢谢!