English 中文(简体)
具有多种模型的铁路应用的Rub
原标题:Ruby on rails application with multiple models

我有几处微薄的ROR“最固定的”型点,正在从事一些更深的工程,我对......没有多少经验。

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

最后,我需要提出一个观点,让用户能够看到自己的项目、档案和记录;说明。 在类似于MANUALLY的名单中或表格中,“概念不受影响”如下:

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

以上清单将使用某种嵌入的垃圾堆,但在我进入需要确保我有适当的模型协会和电话之前。

我试图从我的所有表格中找到无外国钥匙,但我已经真正地与多模式的最佳做法混为一谈。 在没有外国钥匙的情况下,I THINK I需要加入一个表格,该表格可以成为一种使用“模型1_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

结束

你们也许会看到;我在这里感到困惑! 我做了一幅辅导,读了一本书;这是我第一次真正的世界应用,它只是一纸空文。

似乎有很多办法可以做到这一点。 我猜测我对我应使用的模式以及我如何把这些模式连接起来给予某种专家指导。

非常赞赏你提供的任何指示或建议。 谢谢!

最佳回答

正确理解

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?

<>0> 账户、账户项目和项目 许多协会都使用用户表格,我理解你们需要。

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.

问题回答

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. 感谢! hopefully I don t encounter any issues with setting this all up.

感谢!





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签