rails 4 activerecord TypeError nil is not a symbol
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
TypeError: nil is not a symbol
Ok I finally found the answer. I made the join table, 'entries' without an id field. This is require if using the join table as a model with extra data on it.
Rails - TypeError: nil is not a symbol nor a string, when updating
If you are trying to update the record in a dependent model, it possibly has only a foreign key, but no primary key. The problem can be solved if you add a line self.primary_key
like this:
class YourDependentRecord < ApplicationRecord
self.primary_key = 'user_id'
end
where 'user_id'
is the primary key in the master model.
Rails: Trying to update an attribute on a join table | getting TypeError: nil is not a symbol nor a string
I believe the join table is not mean to have a primary key when you're using HABTM.
Since you're storing data on the join table you are, appropriately, using has_many :through
. In this case, you should use a proper table (including id
) for the join table.
BTW, I would think your join model might be UserAssignment
with the table being user_assignments
which, IMO, reads a little more naturally. In which case, you would do:
class Assignment < ActiveRecord::Base
has_many :user_assignments
has_many :assignees, through: :user_assignments, source: :user
end
class User < ActiveRecord::Base
has_many :user_assignments
has_many :assignments, through: :user_assignments
end
class UserAssignment < ActiveRecord::Base
# == Schema Information
# completed_on :datetime
belongs_to :user
belongs_to :assignment
end
Rails 4 to_json produces unexpected Exception nil is not a symbol
The view backing my QuestionAnswerResponse model does not have an id
column and I was not setting a primary key in the model. In the capacity that I use this model I do not need a primary key; this is a read only view used to more easily access some complex key/value pairings directly instead of through more complex logic.
In Rails 3 this worked fine; in Rails 4 when you access a model without a primary key you end up with an attribute that looks like this in your hash nil => nil
The problem is actually up at the ActiveRecord level, but wasn't actually causing a problem until I attempt to do json serialization; at which point in time an attempt is made to call nil.to_sym which raises the exception.
This seems like a bug in ActiveRecord to me; but for now I have worked around it by manually setting a primary key on my model.
TypeError : nil is not a symbol render :json
I solved this issue , this caused because of there is no primary key in the table ,
This worked after i add primary key in the model
class Supplier < ActiveRecord::Base
self.primary_key = 'id'
......
....
end
Rails Polymorphic Association, causing 'nil is not a symbol' TypeError from form_for when trying to create new
The problem was some of the tables had :id => false. Let this be a warning to would be users of db:schema:dump
How to conditionally avoid 'nil can't be coerced into Integer' error
Because some of the account
records have nil
amount, you can convert them to integer
by calling to_i
or to float by to_f
, here account.amount
is returning nil somewhere, do this
account_total += account.amount.to_i
It will resolve the error. But since amount
is a numeric column, you should better set the default value as 0 in migrations at DB level.
or, use sum
account_total = @user.accounts.sum(:amount)
It avoids nil
values and runs a SQL sum
Related Topics
Initialize a Ruby Class from an Arbitrary Hash, But Only Keys with Matching Accessors
What Are All the "Conventions" for Ruby on Rails
Exception_Notification for Delayed_Job
Using Activemodel::Serializer in Rails - JSON Data Differs Between JSON and Index Response
Log Doesn't Work in Production with Delayed Job
Rails Routes: Get Without Param :Id
In Ruby, How to Find Out If a String Is Not in an Array
Ruby -V Dyld: Library Not Loaded: /Usr/Local/Lib/Libgmp.10.Dylib
Is It a Good Idea to Purge Old Rails Migration Files
Rails 3.2.8 - How to Get the Week Number from Rails
Ruby: Escaping Special Characters in a String
Saml 2.0 Sso for Ruby on Rails
In Ruby How to Automatically Populate Instance Variables Somehow in the Initialize Method