Rails 5.0.2
にて開発しています。
とてもレベルの低い質問ですが、Lesson
とSituation
の2つのmodel
がhas_and_belongs_to_many
で関連付
いて居ます。Lesson
がSituation
のタグ
を持っているイメージです。
"コスプレ"
と"日常"
といったsituation
を持つLesson
を取り出す際に、以下の通りwhere
メソッドを書いて居ます。あまりに冗長で見た目が悪いのですが、本来もっと綺麗にかける筈と思うも、書き方わからず、御指南いただけますと幸甚です。
ruby
1#### lessons_controller.rb 2@lessons_ofice = Lesson.includes(:situations).where(situations: {name: "日常"}).or(Lesson.includes(:situations).where(situations:{name: "コスプレ"})) 3
ruby
1#### Lesson Model 2## Table name: lessons 3# 4# id :integer not null, primary key 5# price :integer 6# title :string 7# place :string 8# created_at :datetime not null 9# updated_at :datetime not null 10 11class Lesson < ApplicationRecord 12 has_and_belongs_to_many :situations 13end
ruby
1#### Situation Model 2## Table name: situations 3# 4# id :integer not null, primary key 5# name :string 6# created_at :datetime not null 7# updated_at :datetime not null 8# 9 10class Situation < ApplicationRecord 11 has_and_belongs_to_many :lessons 12end

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/29 11:33