0

したがって、これは非常に悪いフォームである可能性があります。私はレールに比較的慣れていません。わからない。

私にはプロジェクト モデルがあり、多くの所有者 (すべてを読み書きできる人) と多くの協力者 (何かを読み書きできる人) が必要です。

私の project.rb ファイルには次のものがあります。

  has_many :project_user_relationships, :dependent => :destroy
  has_many :collaborators, :through => :project_user_relationships, :source => :user

  has_many :project_owners_relationships, :class_name => "ProjectUserRelationship", :foreign_key => "project_id", 
           :before_add => Proc.new { |p,owner_r| owner_r.owner = true }, :conditions => "`project_user_relationships`.owner = true"
  has_many :owners, :through => :project_owners_relationships, :source => :user

したがって、これはかなりうまく機能します。新しい所有者を追加すると、そのユーザーは私が望むコラボレーターでもあります。解決方法がわからない問題は、既にコラボレーターであるユーザーを所有者として追加すると、結合テーブルに 2 つのエントリが作成されることです。私はそれがすでにそこにある記録を修正することを望みます. それ、どうやったら出来るの?

4

1 に答える 1

1

このために私が提案するデータモデルは次のとおりです。

class Project < ActiveRecord::Base
    has_many :memberships, :dependent => :destroy
    ...
end

class Membership < ActiveRecord::Base
    belongs_to :project
    belongs_to :user
    ...
end

class User < ActiveRecord::Base
    has_many :memberships, :dependent => :destroy
    has_many :projects, :through => :memberships
    ...
end

そして、メンバーシップ テーブルには次の属性が含まれます。

:id
:user_id
:project_id
:is_owner (boolean)

メンバーシップ クラスで定義されたスコープ:

scope :owner, where("is_owner")

User インスタンスの特別なメソッド:

def owned_projects
    memberships.owner.includes(:projects).inject([]) {|array, m| array << m.project; array}
end

user.owned_projects 呼び出しを使用して、ユーザーが所有するプロジェクトを取得できます。

user.projects を呼び出すだけで、共同作業または所有しているユーザーのプロジェクトを確認できます。

このデータ モデルを使用すると、データの正規化が向上し、ユーザーがプロジェクト オーナーであるかどうかを定義する単純なブール属性が得られます。

このデータ モデルは、s/Project/Group/ を除いて、このプロジェクトで使用されます。プロジェクトへのユーザーの招待を処理するための追加機能がいくつかあります。

これはあなたの「本当の質問」には答えませんが、問題の一部は、冗長性と2つの別々のテーブルを管理する必要性を最小限に抑えるために、共同編集者が所有者であるデータモデルが同じテーブルに格納される必要があることだと思います.

于 2012-02-04T02:12:36.693 に答える