

マルチポストです  
[参考](https://teratail.com/questions/302481)  
検索機能でアソシエーションを組んでいる他モデルの情報を検索対象にしたいと思っています

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

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

####テーブル関係  
tweetsテーブル  
|id|text|school_a_id|school_b_id  
|1 |おはよう|1|2|

categoriesテーブル  
|id|name|  
|1|A高校|  
|2|B高校

**検索機能**

tweet.rb

[code]

    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'
    
[/code]

category.rb

[code]

    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
    
[/code]

schema

[code]

      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
    
[/code]

**エラー**

[code]

    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高校%'))
    
[/code]

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


CC BY-SA 4.0
2020-11-07T01:24:45.367
71779
2020-11-07T01:24:45.367
42029
question
ruby-on-rails
Rails 検索機能　他モデル