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

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

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

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Ruby on Rails

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

Q&A

解決済

1回答

634閲覧

Rails アソシエーションについて 購入者と売却者に分けたい

asatonn

総合スコア1

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/06/01 08:48

前提・実現したいこと

購入者と売却者の情報を取り出すためこのようなアソシエーションを組んだのですが、うまくいきません。
どこの部分がうまくいっていないのでしょうか??
ぜひ皆様の知恵をお貸しください。
この記事を参考にしました。
https://qiita.com/fishmans0120/items/5b40fb2a057e4d722a67

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

エラーメッセージ 2.5.3 :002 > user.bought_items Traceback (most recent call last): 1: from (irb):2 NameError (uninitialized constant User::Posts)

該当のソースコード

post.rb class Post < ApplicationRecord belongs_to :user belongs_to :seller, class_name: "User", foreign_key: "seller_id" belongs_to :buyer, class_name: "User", foreign_key: "buyer_id", optional: true has_one :post has_many :favorites, dependent: :destroy has_many :fav_posts, through: :favorites, source: :user mount_uploader :note, NoteUploader validates :title,:content,:teacher_name,:subject,:price,:note, presence: true attachment :post_image end
user.rb class User < ApplicationRecord has_many :posts has_many :sold_items, class_name: 'Posts', foreign_key: 'seller_id' has_many :bought_items, class_name: 'Posts', foreign_key: 'buyer_id' has_many :favorites, dependent: :destroy has_many :fav_posts, through: :favorites, source: :post has_one :card, dependent: :destroy # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :username, :user_image, presence: true attachment :user_image def already_favorited?(post) self.favorites.exists?(post_id: post.id) end end
class PostsController < ApplicationController before_action :authenticate_user!, only: [:new,:create,:edit,:update,:destroy,:done,:purchase,:buy] before_action :find_post, only: [:show, :edit, :update, :destroy, :purchase, :buy,:done] before_action :correct_user, only: [:destroy,:edit] require "payjp" def index @posts = Post.order(id: :desc).page(params[:page]).per(8) end def show end def new @post = Post.new end def create @post = current_user.posts.build(post_params) if @post.save flash[:success] = "正常に投稿されました!" redirect_to posts_path else flash.now[:danger] = "正常に投稿できませんでした。" render :new end end def edit end def update if @post.update(post_params) flash[:success] = 'ノートは正常に更新されました' redirect_to root_path else flash.now[:danger] = 'ノートは更新されませんでした' render :edit end end def destroy @post.destroy flash[:success] = 'メッセージを削除しました。' redirect_to root_path end def purchase # card = Card.where(user_id: current_user.id).first # if card.blank? # redirect_to controller: "cards", action: "new" # else # Payjp.api_key = ENV["PAYJP_SECRET_KEY"] # customer = Payjp::Customer.retrieve(card.customer_id) # @card = customer.cards.retrieve(card.card_id) # end end def buy Payjp.api_key = ENV["PAYJP_SECRET_KEY"] Payjp::Charge.create( amount: @post.price, card: params['payjp-token'], currency: 'jpy' ) @post.update(buyer_id: current_user.id) redirect_to action: "done" end def done end private def post_params params.require(:post).permit(:content,:title,:teacher_name,:subject,:post_image,:note,:price).merge(seller_id: current_user.id) end def find_post @post = Post.find(params[:id]) end def correct_user @post = current_user.posts.find_by(id: params[:id]) unless @post redirect_to new_user_session_path end end end

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

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

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

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

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

winterboum

2021/06/01 08:51

エラーメッセージ全文載せてください。 重要な情報がおちてるので
asatonn

2021/06/01 08:58

こちらコンソールなのでこちらしかエラー文は出ていません。
guest

回答1

0

ベストアンサー

ああ、irbか

has_many :sold_items, class_name: 'Posts', foreign_key: 'seller_id'
has_many :bought_items, class_name: 'Posts', foreign_key: 'buyer_id'

これらの Posts を Post に直して

投稿2021/06/01 08:53

winterboum

総合スコア23397

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

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

asatonn

2021/06/01 08:57

回答ありがとうございます。 ご指摘のように修正したのですが治りません、、他にどのような可能性があるのでしょうか??
maisumakun

2021/06/01 09:02

> ご指摘のように修正したのですが治りません 書き換え後にirbは再起動しましたか?
asatonn

2021/06/01 09:06

再起動したところ治りました!!本当にありがとうございます!! class_name のところはいかなる時でも単数形という認識で大丈夫なのでしょうか??
winterboum

2021/06/01 09:13

いえ、app/models/なんとか.rb で class なんとか としますが、いかなる時もその「なんとか」です。 むりやり 複数形の classを作れないこともないので。
asatonn

2021/06/01 12:40

なるほどですね! ありがとうございます!知識が増えました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問