質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

751閲覧

Rails 検索機能 他モデル

Meitoku

総合スコア44

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/11/05 06:51

編集2020/11/07 12:27

検索機能でアソシエーションを組んでいる他モデルの情報を検索対象にしたいと思っています

####やりたいこと
検索欄にcategoriesテーブルのnameカラムの値を検索すると、それに紐づくツイートを検索結果に表示させたい

例 A高校と検索するとid1のtweetが表示させる

####テーブル関係
tweetsテーブル

idtextschool_a_idschool_b_id
1おはよう12

categoriesテーブル

1name
1A高校
2B高校

####検索機能

tweet.rb

class Tweet < ApplicationRecord def self.search(search) self.joins(:categories).where('text LIKE(?) OR category.name LIKE(?)',"%#{search}%","%#{search}%") end validates :title_info,length: {maximum: 30} validates :tournament_id,:school_a,:school_b,:school_a_score,:school_b_score,:text,:title_info ,presence: true mount_uploader :image, ImageUploader belongs_to :user has_many :category has_many :comments has_many :likes,dependent: :destroy has_many :liked_users,through: :likes,source: :user has_many :notifications,dependent: :destroy belongs_to :school_a,class_name: 'Category', foreign_key: 'school_a_id' belongs_to :school_b,class_name: 'Category', foreign_key: 'school_b_id' belongs_to :tournament,class_name: 'Category', foreign_key: 'tournament_id'

category.rb

class Category < ApplicationRecord has_many :tweets has_many :analyses has_many :forecasts has_ancestry has_many :school_a_tweets,class_name: 'Tweet', foreign_key: 'school_a_id' has_many :school_b_tweets,class_name: 'Tweet', foreign_key: 'school_b_id' has_many :tournament_tweets,class_name: 'Tweet', foreign_key: 'tournament_id' has_many :school_analyses,class_name: 'Analysis', foreign_key: 'school_id' has_many :tournament_analyses,class_name: 'Analysis', foreign_key: 'tournament_id' has_many :win_school_forecasts,class_name: 'Forecast', foreign_key: 'win_school_id' has_many :lose_school_forecasts,class_name: 'Forecast', foreign_key: 'lose_school_id' has_many :tournament_forecasts,class_name: 'Forecast', foreign_key: 'tournament_id' end

schema

create_table "categories", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.string "ancestry" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "tweets", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "title_info" t.text "text" t.text "image" t.integer "school_a_score" t.integer "school_b_score" t.datetime "created_at" t.datetime "updated_at" t.bigint "school_a_id", null: false t.bigint "school_b_id", null: false t.bigint "tournament_id", null: false t.integer "user_id" t.index ["school_a_id"], name: "index_tweets_on_school_a_id" t.index ["school_b_id"], name: "index_tweets_on_school_b_id" t.index ["tournament_id"], name: "index_tweets_on_tournament_id" end

###エラー

Mysql2::Error: Unknown column 'categories.tweet_id' in 'on clause': SELECT `tweets`.* FROM `tweets` INNER JOIN `categories` ON `categories`.`tweet_id` = `tweets`.`id` WHERE (categories.name LIKE('%A高校%') OR text LIKE('%A高校%'))

#####推測
エラー内容から、categoriesテーブルにtweetテーブルと紐づくIDがないからだと思っていますが、仕様上categoriesテーブルにtweet_idなどのカラムを増やすことはできません
アソシエーションの問題だと思いますが、tweet.rbのコードを変えるだけで実装する方法はないのでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

winterboum

2020/11/05 08:21

「アソシエーションを組んでいる他モデル」とありますが、どういう書き方をしているかモデルのcodeを載せてください。 及びそれらのschemaを
Meitoku

2020/11/05 09:28

修正しました どこが必要な情報がよく分からないので、そのまま載せ余計な情報が多いと思いますが、よろしくお願いします
winterboum

2020/11/05 09:38

「検索欄にschoolsテーブルのnameカラムの値を検索すると、」とありますが、codeにschoolsテーブル が見当たりませんが
guest

回答1

0

Category の has_many :tweets が悩ましいです。
tweetからは 'school_a_id' 'school_b_id' のふた通りで関連つけられていますから、Categoryもそれに応じる必要があります

has_many :school_a_tweets, class_name: "Tweet", foreign_key: 'school_a_id'
の様に

投稿2020/11/05 09:43

winterboum

総合スコア23284

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問