私は、ユーザー、ルーム、製品でRoRアプリケーションを構築しようとしています。 私のモデルは以下の通りです。Ruby on Rails:belongs_to/has_many assosiationの製品リスト
class Product < ActiveRecord::Base
belongs_to :user
validates :name, :user_id, presence: true
validates :price, numericality: { greater_than_or_equal_to: 0 }, presence: true
end
class User < ActiveRecord::Base
has_many :products, dependent: :destroy
has_many :memberships, dependent: :destroy
has_many :rooms, through: :memberships
<...>
end
class Room < ActiveRecord::Base
has_many :memberships
has_many :users, through: :memberships
validates :name, presence: true, length: { in: 6..20 }
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :room
validates :user_id, presence: true
validates :room_id, presence: true
end
ユーザーは多くの製品を所有でき、ルームに参加/退室することができます。
私の意見私はcurrent_userの部屋(複数の製品を持つ複数のユーザー)を表示し、そのルームにいるすべてのユーザー(およびcreated_at DESCで並べ替える)すべての製品を一覧表示したいと考えています。これを達成する良い方法を見つけるのを助けてくれますか?
ありがとうございます!
ようこそstackoverflow。 Web上にチュートリアルの100があり、これを行う方法を示しています。そのうちの1つを見てください。特定の質問がある場合は、ここに投稿してください。 – Iceman