0

私はいくつかの小さなROR「ほとんど静的」タイプのサイトを持っており、あまり経験のないもう少し深いものに取り組んでいます...

BIG ピクチャーの命名法。アカウントには多くのユーザーとプロジェクトがあります。ユーザーは多くのプロジェクトを持っています。ユーザーはプロジェクトに多くのファイルとメモを追加します....

最後に、ユーザーがプロジェクト、ファイル、およびメモを表示できるビューを生成する必要があります。以下の手動で作成された「概念実証」に類似したリストまたは表:

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

終わり

ご覧のとおり。ここで迷った!私はたくさんのチュートリアルを行い、本を読みました。これは、主に静的なページだけではない、私の最初の実世界のアプリケーションです....

これを行う方法はたくさんあるようです。どのモデルを使用し、どのように接続する必要があるかについて、専門家の指示を探していると思います。

あなたが提供できる指示やアドバイスは大歓迎です。ありがとうございました!

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

これは、AssociationがRailsでどのように機能するかを説明するのに役立ちますか?

AccountUser、AccountProject、およびProjectUserテーブルはすべて、必要だと理解した多対多の関連付けに使用されます。

スルーアソシエーションは、他の属性をアソシエーションのマッピングに関連付ける場合に役立ちます(たとえば、アカウントとユーザーの両方に関連するもの)。

カスタム属性を必要とせずに単純な多対多の関係が必要な場合は、このhas_and_belongs_to_manyアプローチを使用できますが、私は通常、前もって:throughオプションを選択します。

于 2011-08-31T17:15:26.693 に答える
0

改行なしのコメント欄がイラつく…

ユーザーは 1 つのアカウント (管理者を除く) にしか所属できないため、あなたの提案を参考にして多対多テーブルを削除し、users テーブルで account_id フィールドを使用すると思います。多対多の accounts_projects についても同じです...

これらすべてのモデルを関連付ける最良の方法は、「belongs_to」および「has_many」を適切なテーブルに格納された外部キーに関連付けることです。あなたが与えてくれた教育と、これらすべてのモデルに関連する現実世界の洞察に感謝します. ありがとう!これをすべて設定しても問題が発生しないことを願っています。

ありがとう!

于 2011-09-02T13:39:12.340 に答える