rails 4 activerecord TypeError nil is not a symbol

后端 未结 2 1677
无人及你
无人及你 2021-01-05 02:36

It appears a table in sql server is placing nil in the select statement. I have no idea how to fix this, and if I switch to any other table, I can easily acquir

相关标签:
2条回答
  • 2021-01-05 03:23

    While I still believe this is a bug in active record, I was able to get around it with the https://github.com/rails-api/active_model_serializers gem. This gem overrides to_json eliminated the need for the bug code in active record to be called.

    anyone else have a better answer?

    Updated Controller:

    class V1::DealsController < ApplicationController
    
        def by_mall_id
          deals = Deal.where(mallid: params[:id]).first
          render json: deals, serializer: V1::DealSerializer
        end
    
    end
    

    My new DealSerializer:

    class V1::DealSerializer < ActiveModel::Serializer
        attributes :MALLID
    
        def MALLID
          object.mallid
        end
    
    end
    
    0 讨论(0)
  • 2021-01-05 03:33

    This is because your table doesn't have a primary_key. You can run a migration like this: (this is rails 5 though)

    class AddFieldToPosaCode < ActiveRecord::Migration[5.0]
      execute 'ALTER TABLE my_table ADD PRIMARY KEY(my_column)'
    end
    
    0 讨论(0)
提交回复
热议问题