0

これらを正しく行っているかどうかはわかりません。

アカウント、ユーザー、イベントの 3 つのモデルがあります。

アカウントにはユーザーのグループが含まれています。各ユーザーは、ログイン用の独自のユーザー名とパスワードを持っていますが、同じアカウントで同じアカウント データにアクセスできます。

イベントはユーザーによって作成され、同じアカウント内の他のユーザーもそれを読み取ったり編集したりできます。

次の移行とモデルを作成しました。


ユーザーの移行

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.integer     :account_id
      t.string      :username
      t.string      :password
      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

アカウントの移行

class CreateAccounts < ActiveRecord::Migration
  def self.up
    create_table :accounts do |t|
      t.string      :name
      t.timestamps
    end
  end

  def self.down
    drop_table :accounts
  end
end

イベントの移行

class CreateEvents < ActiveRecord::Migration
  def self.up
    create_table :events do |t|
      t.integer     :account_id
      t.integer     :user_id
      t.string      :name
      t.string      :location
      t.timestamps
    end
  end

  def self.down
    drop_table :events
  end
end

アカウント モデル

class Account < ActiveRecord::Base
  has_many      :users
  has_many      :events
end

ユーザーモデル

class User < ActiveRecord::Base
  belongs_to    :account
end

イベントモデル

class Event < ActiveRecord::Base
  belongs_to    :account
  belongs_to    :user
end

それで....

  1. この設定は正しいですか?
  2. ユーザーが新しいアカウントを作成するたびに、システムはユーザー情報 (ユーザー名やパスワードなど) を尋ねます。それらを正しいテーブルに追加するにはどうすればよいですか?
  3. 新しいイベントを追加するにはどうすればよいですか?

このような長い質問で申し訳ありません。このようなデータ構造を処理するレールの方法がよくわかりません。私に答えてくれてありがとう。:)

4

1 に答える 1

2

これはhas_many :throughの仕事のように見えます(下にスクロールして:throughオプションを見つけます)

イベントを作成したユーザーを知る必要がある場合は、イベントが実際にユーザーのみに属していることを指定する必要があります。

class Event < ActiveRecord::Base
  belongs_to    :user
end

ただし、アカウントはユーザーのイベントを「取得」できます。次のように指定します。

class User < ActiveRecord::Base
  belongs_to :account
end

class Account < ActiveRecord::Base
  has_many :users
  has_many :events, :through => :users
end

Account移行は、 と について書いたものと同じですUserEventあなたは削除することができますaccount_id

class CreateEvents < ActiveRecord::Migration
  def self.up
    create_table :events do |t|
      t.integer     :user_id
      t.string      :name
      t.string      :location
      t.timestamps
    end
  end

  def self.down
    drop_table :events
  end
end

次に、イベントを次のように作成できます。

# These two are equivalent:
event = user.events.create(:name => 'foo', :location => 'bar')
event = Event.create(:user_id => user.id, :name => 'foo', :location => 'bar')

これにより、イベントがすぐに作成および保存されることに注意してください。イベントを保存せずに作成する場合は、代わりにuser.events.buildorを使用できますEvent.new

has_many :throughon Accounts を使用すると、1 つのアカウントのすべてのイベントを取得できます。

user.events         # returns the events created by one user
account.events      # returns all the events created by the users of one account
user.account.events # returns the events for the user's account

最後の注意として、ここで車輪を再発明していることに注意してください。ユーザーと権限を管理するための非常に優れたソリューションがあります。

アカウントの管理には devise ( railscast ) または authlogic ( railscast ) を、権限管理declarative_authorization ( railscast )またはcancan ( railscast )を検討することをお勧めします。私の個人的な選択は、devise と declarative_authorization です。前者は authlogic よりインストールが簡単で、後者は cancan より強力です。

よろしく、そして頑張ってください!

于 2010-05-05T09:18:31.580 に答える