Rails多态关联未初始化常量误差

时间:2015-01-16 00:42:19

标签: ruby-on-rails activerecord associations polymorphic-associations uninitialized-constant

一直在调试这几个小时但仍无法找到解决方案。当我在事务控制器的update方法中发送调用@ transaction.save的PUT请求时,我的服务器日志会吐出:

NameError - uninitialized constant Transaction::Respondable:

基本相关架构是用户和组织都可以拥有请求,请求可以拥有交易,用户和组织也可以拥有交易。区别在于,在初始化请求的人/组织1和响应请求的人/组织2之间存在交易。换句话说,一个Transaction同时属于一个Request(并通过一个请求User / Orgnization 1)和User / Organization 2.我现在只是在用户那里寻找,所以我现在不会包含有关Organizations的代码,因为它是尚未实现。这是相关的代码:

架构:

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是指Request(也是多态的,因为它也可以来自Offer,但现在不相关)。可响应的是指用户。

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

用户架构无关紧要,因为它不包含指向其他表的链接(只有关系是has_many)。

这是我的模特:

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

和TransactionsController:

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

最奇怪的是,在Rails控制器中,调用User.first.responded_transactions或Transaction.first.respondable这样的调用。然而似乎某些与可响应相关的东西在某个地方失败了...任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:4)

问题出在验证上。尝试单独验证您的多态列。让我告诉你:

validates(
  :respondable_id, 
  uniqueness: { 
    scope: [:respondable_type, :listable_id, :listable_type], 
    message: "You've already initialized a transaction on this item" 
  }
)