Ruby on rails Rails多态关联未初始化常量错误
已经调试了几个小时,仍然找不到解决方案。当我在事务控制器的更新方法中发送调用@transaction.save的PUT请求时,我的服务器日志会显示:Ruby on rails Rails多态关联未初始化常量错误,ruby-on-rails,activerecord,associations,polymorphic-associations,uninitialized-constant,Ruby On Rails,Activerecord,Associations,Polymorphic Associations,Uninitialized Constant,已经调试了几个小时,仍然找不到解决方案。当我在事务控制器的更新方法中发送调用@transaction.save的PUT请求时,我的服务器日志会显示: NameError - uninitialized constant Transaction::Respondable: 基本的相关模式是用户和组织都可以有请求,请求可以有事务,用户和组织都可以有事务。区别在于事务存在于初始化请求的人员/组织1和响应请求的人员/组织2之间。换句话说,事务同时属于请求(并通过请求用户/组织1)和用户/组织2。我现在
NameError - uninitialized constant Transaction::Respondable:
基本的相关模式是用户和组织都可以有请求,请求可以有事务,用户和组织都可以有事务。区别在于事务存在于初始化请求的人员/组织1和响应请求的人员/组织2之间。换句话说,事务同时属于请求(并通过请求用户/组织1)和用户/组织2。我现在只是在寻找用户,所以我现在不包括关于组织的代码,因为它还没有实现。以下是相关代码:
模式:
create_table "transactions", force: true do |t|
t.string "name", null: false
t.string "description", null: false
t.integer "listable_id", null: false
t.string "listable_type", null: false
t.integer "respondable_id", null: false
t.string "respondable_type", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
listable指的是请求(也是多态的,因为它也可以来自一个要约,但现在不相关)。respondable指的是用户
create_table "requests", force: true do |t|
t.string "name", null: false
t.string "description", null: false
t.integer "requestable_id", null: false
t.string "requestable_type", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
用户模式是不相关的,因为它不包含指向其他表的链接(只有关系是多个)
以下是我的模型:
class User < ActiveRecord::Base
...
has_many :requests, as: :requestable, dependent: :destroy
has_many :responded_transactions, class_name: 'Transaction', as: :respondable, dependent: :destroy
has_many :listed_transactions, through: :requests, source: :transactions
...
end
class Request < ActiveRecord::Base
validates :name, :description, :requestable, presence: true
has_many :transactions, as: :listable, dependent: :destroy
belongs_to :requestable, polymorphic: true
def relevant_transaction(user)
self.transactions.where('respondable_id = ?', user.id).first
end
end
class Transaction < ActiveRecord::Base
validates :name, :description, :listable, :respondable, presence: true
validates :respondable, uniqueness: { scope: :listable, message: 'You have already initialized a transaction on this item' }
belongs_to :listable, polymorphic: true
belongs_to :respondable, polymorphic: true
before_validation :set_name_and_description
private
def set_name_and_description
self.name = listable.name
self.description = listable.description
end
end
class用户
以及TransactionController:
module Api
class TransactionsController < ApplicationController
def create
@transaction = current_user.responded_transactions.new(transaction_params)
if @transaction.save
render :show, status: :created
else
render json: @transaction.errors.full_messages, status: :unprocessable_entity
end
end
def update
@transaction = Transaction.find(params[:id])
if @transaction.save
render :show, status: :saved
else
render json: @transaction.errors.full_messages, status: :unprocessable_entity
end
end
private
def transaction_params
params.require(:transaction).permit(:listable_id, :listable_type)
end
end
end
模块Api
类TransactionController
最奇怪的是,在Rails控制器中,调用User.first.responded\u事务或Transaction.first.respondeable工作。然而,似乎与respondable相关的东西在某个地方失败了。。。任何帮助都将不胜感激 问题在于您的验证。尝试分别验证多态列。让我告诉你:
validates(
:respondable_id,
uniqueness: {
scope: [:respondable_type, :listable_id, :listable_type],
message: "You've already initialized a transaction on this item"
}
)
您还需要显示发布到相关保存方法的表单。另外,你能去掉任何不相关的代码吗(例如,这是在创建还是更新中发生的?去掉另一个)。此外,如果您可以提供完整的stacktrace(而不仅仅是单个错误),这将有所帮助。这应该是20-30行代码,显示发生错误的文件的行号(应该在服务器日志中)。