モデル「Trip」があり、「Group」の特定のユーザーだけに「comments」を作成させたい。
旅行モデルを最初に作成したときは、1人のユーザーだけが編集できるように設定していました。さて、他のユーザーにも編集してもらいたいです。今私をつまずかせている(しゃれを意図している)部分は、私がtrip belong_to :user
(それを作成したユーザー)との両方を持っているということtrip has_many :users, :through => :group
です。
質問:
- これはRailsの規則に従って許可されていますか?
- 私のモデルに基づくと、グループには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 %>