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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

837閲覧

文字のみの投稿の際、ハッシュタグをつけて保存したいがundefined method `find_by' for ActiveRecord::AttributeMethodsのエラー。

nao---

総合スコア1

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/03/26 06:47

編集2021/03/26 07:09

前提・実現したいこと

railsまだまだ勉強中の者です。
今文字や画像などを投稿する際、ハッシュタグ機能を付けようとしています。
画像投稿の際は、ちゃんとエラーも起こらずハッシュタグ機能をつけることができました。

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

エラーメッセージ undefined method `find_by' for ActiveRecord::AttributeMethods::Write:Module after_create do write = Write.find_by(id: self.id) tags = self.summary.scan(/[##][\w\p{Han}ぁ-ヶヲ-゚ー]+/)    tags.uniq.map do |t| tag = Tag.find_or_create_by(name: t.downcase.delete('#')) write.tags << tag end #エラーとなっている部分はwrite = Write.find_by(id: self.id)
class Write < ApplicationRecord belongs_to :user has_many :comments has_many :likes, dependent: :destroy has_many :likes_users, through: :likes, source: :user has_many :post_tags, dependent: :destroy has_many :tags, through: :post_tags after_create do write = Write.find_by(id: self.id) tags = self.summary.scan(/[##][\w\p{Han}ぁ-ヶヲ-゚ー]+/) tags.uniq.map do |t| tag = Tag.find_or_create_by(name: t.downcase.delete('#')) write.tags << tag end end end
class WritesController < ApplicationController before_action :move_to_index, except: [:index, :show] def index @write = Write.includes(:user).order("created_at DESC") end def new @write = Write.new end def create @write = Write.new(write_params) if @write.user_id = current_user.id @write.save redirect_to action: :index end end def destroy write = Write.find(params[:id]) if write.user_id == current_user.id write.destroy end end def edit @writes = Write.find(params[:id]) if @writes.user_id != current_user.id redirect_to "/writes" end end def update write = Write.find(params[:id]) if write.user_id == current_user.id write.update(write_params) end end def show @writes = Write.find(params[:id]) @comment = Comment.new @comments = @writes.comments.order(created_at: :desc) @com = "write" @like = Like.new end private def write_params params.require(:write).permit(:title, :content, :summary, :tag_ids) end def move_to_index redirect_to action: :index unless user_signed_in? end end
#投稿の際のform <div class="posts-wrapper-content">  <div class="preview" id="image"> <%= form_with model: @write, local: true do |w| %> <div class="write-content"> <%= w.text_area :content, placefolder: "story" %> </div> </div> <div class="information-content"> <div class="info-cover"> <div class="title-form"> :タイトルを入力してください </div> <%= w.text_field :title %> <div class="content-wrapper"> :内容を入力してください。 </div> <%= w.text_area :summary, class: 'content-form' %> <%= w.submit "投稿する", class: 'send-button' %> <% end %> </div> </div> </div>
class Image < ApplicationRecord belongs_to :user has_one_attached :image, dependent: :destroy has_many :likes, dependent: :destroy has_many :likes_users, through: :likes, source: :user has_many :comments has_many :post_tags, dependent: :destroy has_many :tags, through: :post_tags def image_type if image.attached? == false errors.add(:image, 'error message') end end after_create do image = Image.find_by(id: self.id) tags = self.content.scan(/[##][\w\p{Han}ぁ-ヶヲ-゚ー]+/) tags.uniq.map do |t| tag = Tag.find_or_create_by(name: t.downcase.delete('#')) image.tags << tag end end before_update do image = Image.find_by(id: self.id) image.tags.clear tags = self.caption.scan(/[##][\w\p{Han}ぁ-ヶヲ-゚ー]+/) tags.uniq.map do |hashtag| tag = Tag.find_or_create_by(name: hashtag.downcase.delete('#')) image.hashtags << tag end end end
class ImagesController < ApplicationController def new @image = Image.new end def index @images = Image.order("created_at DESC") end def create @image = Image.new(image_params) @image.user_id = current_user.id if @image.image.attached? == false @error = "画像ファイルを貼り付けてください" render :new else @image.save redirect_to action: :index end end def destroy image = Image.find(params[:id]) if image.user_id == current_user.id image.destroy end redirect_to action: :index end def show @image = Image.find(params[:id]) @user = User.find(@image.user_id) @like = Like.new @comment = Comment.new @comments = @image.comments.order(created_at: :desc) end def edit @image = Image.find(params[:id]) end def update @image = Image.find(params[:id]) @image.update params.require(:image).permit(:title, :content, :image) # POINT redirect_to @image end private def image_params params.require(:image).permit(:title, :content, :image, :tag_ids) end end

補足情報(FW/ツールのバージョンなど)

Ruby 2.6.6
Rails 5.2.4.5

エラー名を調べていてもどこが悪いのかいまいちよくわかってない状態です。
特に画像投稿の場合は、何も問題なく投稿できているのでさらに混乱しています。
どなたかご教授いただきたいです。

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

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

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

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

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

guest

回答1

0

ベストアンサー

モデル名 Write がだめっぽい
ActiveRecord::AttributeMethods::Write:Module
とあるので、systemに Writeというのがあってそちらを見に行ってしまってるのでしょう。

改名しましょう。

モデルに動詞を使うのには違和感ありなのですが、、、
どんなものを表すモデル?

やっぱ予約語ですね、Writeは

投稿2021/03/26 10:47

編集2021/03/26 10:49
winterboum

総合スコア23403

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

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

nao---

2021/03/26 11:28

あーそうだったんですね。 改名して試してみます。 ありがとうございます。
nao---

2021/03/26 15:15

テーブル名を変更したら無事ちゃんとハッシュタグをつけることができました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問