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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

Q&A

解決済

1回答

297閲覧

タグにユーザー情報を紐付けられない

AKIRA0310

総合スコア15

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

0グッド

0クリップ

投稿2022/04/30 14:21

編集2022/04/30 23:29

前提・実現したいこと

タグ検索の機能を実装したいのですが、タグにユーザー情報を紐づけることができず、検索結果を表示できません。

発生している問題・エラーメッセージ

イメージ説明
該当のソースコード

routes.rb

ruby

1devise_for :users 2 root to: "profiles#index" 3 resources :profiles 4 resources :users, only: [:edit, :update] 5 resources :messages, only: [:create] 6 resources :rooms, only: [:create, :show] 7 resources :tags do 8 get "profiles", to: "profiles#search" 9 end

profile.rb

ruby

1class Profile < ApplicationRecord 2 mount_uploader :image, ImageUploader 3 4belongs_to :user 5 has_many :room_users, dependent: :destroy 6 has_many :rooms, through: :room_users 7 has_many :messages, dependent: :destroy 8 has_many :profile_tags, dependent: :destroy 9 has_many :tags, through: :profile_tags 10 11 validates :nickname, presence: true 12 13 def save_tag(sent_tags) 14 current_tags = self.tags.pluck(:tag_name) unless self.tags.nil? 15 old_tags = current_tags - sent_tags 16 new_tags = sent_tags - current_tags 17 18 old_tags.each do |old| 19 self.tags.delete Tag.find_by(tag_name: old) 20 end 21 22 new_tags.each do |new| 23 new_profile_tag = Tag.find_or_create_by(tag_name: new) 24 self.tags << new_profile_tag 25 end 26 end 27end

tag.rb

ruby

1class Tag < ApplicationRecord 2 has_many :profile_tags, dependent: :destroy 3 has_many :tags, through: :profile_tags 4end

profile_tag.rb

ruby

1class ProfileTag < ApplicationRecord 2 belongs_to :profile 3 belongs_to :tag 4end

profiles_controller

ruby

1before_action :set_profile, only: [:show, :edit, :update, :destroy] 2 def index 3 @profiles = Profile.all 4 @tag_list = Tag.all 5 end 6 7 def new 8 @profile = Profile.new 9 end 10 11 def create 12 @profile = Profile.new(profile_params) 13 tag_list = params[:profile][:tag_name].split(nil) 14 if @profile.save 15 @profile.save_tag(tag_list) 16 redirect_to root_path 17 else 18 render :new 19 end 20 end 21 22 def show 23 @profile_tags = @profile.tags 24 @sendUser = RoomUser.where(profile_id: current_user.id) 25 @receiveUser = RoomUser.where(profile_id: @profile.id) 26 27 unless @profile.user_id == current_user.id 28 @sendUser.each do |su| 29 @receiveUser.each do |ru| 30 if su.room_id == ru.room_id 31 @isRoom = true 32 @roomId = su.room_id 33 end 34 end 35 end 36 unless @isRoom 37 @room = Room.new 38 @roomUser = RoomUser.new 39 end 40 end 41 end 42 43 def edit 44 @tag_list = @profile.tags.pluck(:tag_name).join(', ') 45 end 46 47 def update 48 tag_list = params[:profile][:tag_name].split(' ') 49 @profile.update(profile_params) 50 if @profile.save 51 @profile.save_tag(tag_list) 52 redirect_to profile_path(@profile.id) 53 else 54 render :edit 55 end 56 end 57 58 def destroy 59 @profile.destroy 60 redirect_to root_path 61 end 62 63 def search 64 @tag = Tag.find(params[:tag_id]) 65 @profiles = @tag.profiles.all 66 end 67 68 private 69 def profile_params 70 params.require(:profile).permit(:image, :nickname, :comment, :content, :twitter, :instagram ).merge(user_id: current_user.id) 71 end 72 73 def set_profile 74 @profile = Profile.find(params[:id]) 75 end

search.html.erb

ruby

1<div class='call-contents'> 2 <h2 class='call-title'> 3 <%= @tag.tag_name %>の検索結果 4 </h2> 5 </div> 6 7 <div class='user-lists'> 8 <div class='user-list'> 9 <% @profiles.each do |profile| %> 10 <div class='user-wrapper'> 11 <%= link_to profile_path(profile.id) do %> 12 <% if profile.image? %> 13 <%= image_tag profile.image.url, :size => '200x200' %> 14 <% else %> 15 <%= image_tag "/assets/default-user-icon.png", :size => '200x200' %> 16 <% end %> 17 <div class='user-body'> 18 <div class='header_comment'> 19 <%= profile.comment %> 20 </div> 21 <div class='user_name'> 22 <%= profile.nickname %> 23 </div> 24 <p class='rating-star'> 25 <%= image_tag "star-on.png" %> 26 </p> 27 </div> 28 <% end %> 29 </div> 30 <% end %> 31 </div> 32 </div>

自分で調べたことや試したこと

profile_tagsテーブルにprofileとtagの外部キーを設定しているので、それぞれに紐づけられるはずなのですがノーメソッドエラーが出てしまいます。各モデルのアソシエーションやカラムを確認したのですが原因は見つけられませんでしたので@profilesの呼び出し方に問題があるような気がしているのですが、どう表記すべきなのでしょうか?どなたかご教授いただければ幸いです。

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

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

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

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

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

guest

回答1

0

ベストアンサー

載っている tag.rb のcodeが本当に tag.rb のものであるなら(class 行がないので判断できない)
has_many :tags, through: :profile_tags が間違いです。

投稿2022/04/30 23:16

winterboum

総合スコア23329

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

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

AKIRA0310

2022/04/30 23:36

ありがとうございます そうですね、has_many :profilesの間違いでした
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問