こんにちは
今、railsで本棚のようなアプリを作っていて、本(posts)に複数のカテゴリーを持てるようにしたいのですが、
中間テーブルにデータを保存する方法や取り出し方?などがさっぱり分かりません。
テーブル同士を紐づける所までは調べながらできたと思います
テーブル
posts id name categories id name classifications post_id category_id ↑この3つを紐づけしましたがもう一つ usersテーブルもあります
該当のソースコード
Classification.rb class Classification < ApplicationRecord belongs_to :post belongs_to :category end Category.rb class Category < ApplicationRecord has_many :classifications has_many :posts, through: :classifications end post.rb class Post < ApplicationRecord has_many :classifications has_many :categories, through: :classifications end
補足情報(FW/ツールのバージョンなど)
Ruby . rails .
どちらもバージョンは最新です。
controller viewの記法など教えてください。
8時間くらい溶かしました。( ノД`)シクシク…
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。