1

モデル「Trip」があり、「Group」の特定のユーザーだけに「comments」を作成させたい。

旅行モデルを最初に作成したときは、1人のユーザーだけが編集できるように設定していました。さて、他のユーザーにも編集してもらいたいです。今私をつまずかせている(しゃれを意図している)部分は、私がtrip belong_to :user(それを作成したユーザー)との両方を持っているということtrip has_many :users, :through => :groupです。

質問:

  1. これはRailsの規則に従って許可されていますか?
  2. 私のモデルに基づくと、グループにはuser_idとtrip_idの両方があります。これはこの問題に取り組むための最良の方法ですか?つまり、グループに招待するすべてのユーザーのデータベースに新しいレコードが必要ですか?

ありがとう。

user.rb

class User < ActiveRecord::Base
  has_many :trips, :through => :groups
  has_many :trips, :dependent => :destroy
  has_many :groups
  has_many :comments, :dependent => :destroy
end

group.rb

class Group < ActiveRecord::Base
  belongs_to :trip
  belongs_to :user
end

trip.rb

class Trip < ActiveRecord::Base
  belongs_to :user
  belongs_to :traveldeal
  has_many :groups
  has_many :users, :through => :groups
end

comment.rb

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :commentable, :polymorphic => true
end

ビュー(show.html.erb)

<% unless @user.trips.empty? %>
  <% @user.trips.each do |trip| %>
    <!-- Content here -->
  <% end %>
<% end %>
4

1 に答える 1

0

通常Railsの慣例では、Groupモデルを結合する2つのテーブルを指定するようなものと呼びTripPersonますが、これらの結合テーブルはリレーショナルデータベースの自然な副産物です。Group旅行に招待されたすべての人のために、新しいオブジェクトを作成し続ける必要があります。

于 2012-04-14T00:26:28.377 に答える