具有多个模型的 Ruby on Rails 应用程序

发布于 2024-12-02 17:07:29 字数 1641 浏览 1 评论 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”命名约定的模型?以及“: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

结束

正如您可能看到的;我在这里很困惑!我已经完成了很多教程并读了一本书;这是我的第一个现实世界的应用程序,它不仅仅是静态页面......

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

非常感谢您提供的任何指导或建议。谢谢你!

I have a few small ROR "mostly static" type sites and am working on something a little deeper that I don't have much experience with...

The BIG picture nomenclature.
Accounts have many users and projects. users have many projects. users add many files and notes to projects....

In the end I need to generate a view where users can see their projects, files, & notes. In a list or table similar to the MANUALLY created "proof of concept" one below:

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

The list above will be generated using some sort of an embedded ruby for loop but before I get into that I need to ensure I have the proper model associations and calls.

I'm attempting to have NO foreign keys from all my tables but I've been getting really confused with multi-model best practices. With no foreign keys I THINK I need a join table, which could be a model using "model1_model2" naming convention?, and the ":through" model relationship?

my models now (this has been changing alot) are:

Account:

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

end

User:

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

end

Project:

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

end

DataFile:

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

end

Note:

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

end

As you can probably see; I'm confused here! I've done a bunch of tutorials and read a book; this is my first real world application that isn't just mainly static pages....

There appears to be a lot of ways this could be done. I guess I'm looking for some expert direction on what models I should use and how I should connect them.

Any direction or advice you can offer is greatly appreciated. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紅太極 2024-12-09 17:07:29

如果我理解正确的话,

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 选项。

If i understood you correctly

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

Does that help explain how the associations will work in rails?

NOTE The AccountUser, AccountProject and ProjectUser tables are all used for the many to many associations I understood you to need.

The through associations are useful if you're ever going to associate any other attributes with the mapping of the associations (something in relation to both the account and user for example).

If you just need a simple many-to-many relationship without the need for custom attributes, you can simply use the has_and_belongs_to_many approach, though I usually go for the :through option up front.

挽你眉间 2024-12-09 17:07:29

无回车注释字段让我很恼火...

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

听起来将所有这些模型关联起来的最佳方法是“belongs_to”和“has_many”与存储在适当表中的外键。我很欣赏您所给予的教育以及对所有这些模型相关的现实世界的洞察力。谢谢!希望我在设置这一切时不会遇到任何问题。

谢谢!

The no carriage return comment field irritates me...

Users can only belong to one account (except for admins) so I think I will take your suggestion and remove the many to many table and use an account_id field in the users table. Same for the accounts_projects many to many...

it sounds like the best way to associate all these models is "belongs_to" and "has_many" with foreign keys stored in the appropriate tables. I appreciate the eduction you've given and the real world insight on associated all these models. Thanks! hopefully I don't encounter any issues with setting this all up.

Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文