12

Simply trying to save with ActiveRecord and it I keep getting "TypeError: nil is not a symbol"

if card_data[:card] and card_data[:deck]
  e = self.find_or_create_by(card_id: card_data[:card].id, deck_id: card_data[:deck].id, main: main)
  e.card = card_data[:card]
  e.deck = card_data[:deck]
  e.quantity = card_data[:quantity].to_i
  e.main = main
  p e
  e.save if e.card and e.deck
end

I run the above code.

Schema:

create_table "entries", id: false, force: true do |t|
  t.integer  "card_id"
  t.integer  "deck_id"
  t.integer  "quantity",   default: 0, null: false
  t.integer  "main",       default: 0, null: false
  t.datetime "created_at"
  t.datetime "updated_at"
end

When I use pry, it won't let me e.save even immediately after I find_or_create_by.

#<Entry card_id: 1, deck_id: 1, quantity: 4, main: 1, created_at: "2014-10-26 00:45:12", updated_at: "2014-10-26 00:45:12">
(0.2ms)  BEGIN
(0.2ms)  ROLLBACK
TypeError: nil is not a symbol
from /home/kevin/.rvm/gems/ruby-2.1.2/gems/activemodel-4.1.6/lib/active_model/dirty.rb:162:in `attribute_was'

Please any help. I've spent hours on this. I tried mysql insteal of sqlite. I tried different column datatypes. The issue is the field 'quantity'. It won't let me save.

Edit: variable 'main' is set above what is shown.

Kevin McCabe
  • 421
  • 4
  • 12
  • 1
    As an aside, you shouldn't be using `and` and `or` in place of `&&` and `||`. They're not the same. See https://github.com/bbatsov/ruby-style-guide#no-and-or-or – user229044 Oct 26 '14 at 02:13

2 Answers2

29

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.

Kevin McCabe
  • 421
  • 4
  • 12
  • 1
    Was this documented anywhere? I tried doing the same with my join tables, but I'm still getting the same error. Also, you should probably accept your own answer. – Attila O. Nov 11 '14 at 20:18
  • 2
    This is mentioned nowhere in any documentation that I have read, and I have googled this extensively as I was plagued by the issue for the past two days. I had a join table that was using `has_and_belongs_to_many` for the association which I migrated to `has_many :through`; the join table still did not have any `id`, so any attempts to update records failed. Now I know why this happened, and I hope that Google brings others with the same issue here as the #1 result. – Chris Cirefice Jan 08 '16 at 00:08
  • 2
    Also, if you don't want to add a primary key you can use the `update_all` method - [see here](http://stackoverflow.com/questions/13263188/how-do-you-update-a-join-table-record-with-no-primary-keys-in-rails). If you're trying to update a specific record, you can do a `where` clause first, then `update_all`. – Robert Jan 28 '16 at 00:25
1

For those of you coming here and needing to add the primary key, you can do it in a migration like this:

class AddIdToMyTable < ActiveRecord::Migration def change add_column :my_table, :id, :primary_key end end

phil
  • 4,668
  • 4
  • 33
  • 51