gpt4 book ai didi

ruby-on-rails - Rails - 导致 "Stack Too Deep"错误的唯一性

转载 作者:数据小太阳 更新时间:2023-10-29 08:56:57 24 4
gpt4 key购买 nike

我目前正在开发一个 API,在我的模型(称为 Roast)中,它属于两个用户。一个是创造者,另一个是接受者。我目前的问题是,我只想要这两个用户可以相互参与的一个实例。我发现一篇 [article][1] 和我创建验证器的问题一样有问题,但我的问题归结为堆栈问题。我不确定这个循环发生在哪里,所以我希望你们能帮助我解决这个问题。

# roast.rb

class Roast < ApplicationRecord
validates :roast_creator, uniqueness: { scope: :roast_receiver }

belongs_to :roast_creator, :class_name => :User, :foreign_key => :roast_creator
belongs_to :roast_receiver, :class_name => :User, :foreign_key => :roast_receiver

has_many :comments, :dependent => :destroy

def last_responder

end

def last_reply
return self.comments.order('comments.created_at DESC').limit(1).load[0] if self.comments.length > 0
end
end

# user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
include DeviseTokenAuth::Concerns::User

has_many :created_roasts, :class_name => :Roast, :foreign_key => :roast_creator
has_many :received_roasts, :class_name => :Roast, :foreign_key => :roast_receiver

has_many :comments
end

# roasts_controller
class RoastsController < ApplicationController
before_action :set_roast, only: [:show, :update, :destroy, :respond]

# GET /roasts
# GET /roasts.json
def index
@roasts = Roast.all
end

# GET /roasts/1
# GET /roasts/1.json
def show
end

# POST /roasts
# POST /roasts.json
def create
@roast = Roast.new
puts "NEW"
@roast.roast_creator = current_user
puts "CREATOR SET"
@roast.roast_receiver = User.find(params[:receiver])
puts "RECEIVER SET"
@comment = @roast.comments.new(user: current_user, content: params[:comment])
puts "INITIAL COMMENT SET"

if @roast.save && @comment.save
puts "SAVE CREATED"
render :show, status: :created, location: @roast
else
render json: @roast.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /roasts/1
# PATCH/PUT /roasts/1.json
def update
if @roast.update(roast_params)
render :show, status: :ok, location: @roast
else
render json: @roast.errors, status: :unprocessable_entity
end
end

# DELETE /roasts/1
# DELETE /roasts/1.json
def destroy
if(@roast.creator == current_user)
@roast.destroy
end
end

def respond
if (current_user.id == @roast.creator.id || current_user.id == @roast.receiver.id)
@comment = @roast.comments.create(user: current_user, content: params[:comment])

if(@comment.save)
render :show, status: :created, location: @roast
else
render json: @comment.errors, status: :unprocessable_entity
end
else
render json: { error: "Current user is not a creator or a receiver of this particular roast." }, status: :unprocessable_entity
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_roast
@roast = Roast.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
end

准确的错误是:

SystemStackError (stack level too deep):

app/controllers/roasts_controller.rb:27:in `create'

提前感谢你们提供的任何见解!

最佳答案

您已经使用这两个关联配置了循环:

 belongs_to :roast_creator, :class_name => :User, :foreign_key => :roast_creator
belongs_to :roast_receiver, :class_name => :User, :foreign_key => :roast_receiver

foreign_key 选项,特别是。这告诉 Rails 调用方法 roast_receiver 来获取外键值,这将调用名为 roast_receiver关联,它具有外键值roast_receiver 将调用关联...等等,等等。

这里的foreign_key选项应该指向roast_receiver_id,而不是roast_receiver:

belongs_to :roast_creator, :class_name => :User, :foreign_key => :roast_creator_id
belongs_to :roast_receiver, :class_name => :User, :foreign_key => :roast_receiver_id

但 Rails 会根据关联的名称自动为您执行此操作,所以实际上您可以只使用这两行:

belongs_to :roast_creator, :class_name => :User
belongs_to :roast_receiver, :class_name => :User

关于ruby-on-rails - Rails - 导致 "Stack Too Deep"错误的唯一性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50957953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com