Why is factory girl picking up the wrong class? - ruby-on-rails

I have a factory girl factory defined as follows:
FactoryGirl.define do
factory :task do
factory :facebook_task, class: Task::Facebook do
# ...
end
factory :twitter_task, class: Task::Twitter do
# ...
end
end
end
for models defined as follows:
class Task < ActiveRecord::Base
end
class Task::Twitter < Task
end
class Task::Facebook < Task
end
It all work fine until I add the twitter gem which clearly defines a ::Twitter module
.../test/factories/tasks.rb:12: warning: toplevel constant Twitter referenced by Task::Twitter
Running via Spring preloader in process 51820
rake aborted!
NoMethodError: undefined method `new' for Twitter:Module
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator/new_constructor.rb:9:in `new'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:14:in `send'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:10:in `method_missing'
Cleaning database...done.
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator/invocation_tracker.rb:11:in `method_missing'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:14:in `send'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:10:in `method_missing'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/configuration.rb:19:in `block in initialize'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:48:in `instance_exec'
Creating Beatles.../Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:48:in `build_class_instance'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:13:in `object'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/evaluation.rb:12:in `object'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy/create.rb:9:in `result'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory.rb:42:in `run'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/notifications.rb:166:in `instrument'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:28:in `run'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
/Users/pupeno/Documents/code/buzz/lib/tasks/sample_data.rake:25:in `block (2 levels) in <top (required)>'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
-e:1:in `<main>'
Tasks: TOP => sample_data:generate
(See full trace by running task with --trace)
Why is Factory Girl trying to instantiate Twitter instead of Task::Twitter?
tasks.rb:12 is this line:
factory :twitter_task, class: Task::Twitter do
I don't see how it's referencing the toplevel constant Twitter.

try to use class name as a string:
factory :facebook_task, class: 'Task::Facebook' do
# ...
end
instead
factory :facebook_task, class: Task::Facebook do
# ...
end

Related

Factorybot doesn't recognize any attributes—"method missing" errors

I recently upgraded a bunch of gems, and since then I can't run rails server, console, or rspec.
Here's the error log I see:
NoMethodError:
undefined method 'name' in 'book' factory
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/definition_proxy.rb:97:in `method_missing'
# ./spec/factories/books.rb:3:in `block (2 levels) in <top (required)>'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:18:in `instance_eval'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:18:in `factory'
# ./spec/factories/books.rb:2:in `block in <top (required)>'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:49:in `instance_eval'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:49:in `run'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:7:in `define'
# ./spec/factories/books.rb:1:in `<top (required)>'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:20:in `block (2 levels) in find_definitions'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:19:in `each'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:19:in `block in find_definitions'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:15:in `each'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:15:in `find_definitions'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/reload.rb:6:in `reload'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot_rails-5.0.2/lib/factory_bot_rails/reloader.rb:25:in `block in build_reloader'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot_rails-5.0.2/lib/factory_bot_rails/reloader.rb:35:in `block in register_reloader'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:3:in `require'
# ./spec/rails_helper.rb:3:in `<top (required)>'
# ./spec/controllers/api/v1/songs_controller_spec.rb:1:in `require'
# ./spec/controllers/api/v1/songs_controller_spec.rb:1:in `<top (required)>'
Part of the upgrade included upgrading rails to 5.2.3, which forced me to remove this line from new_framework_defaults.rb file:
# Do not halt callback chains when a callback returns false. Previous versions had true.
ActiveSupport.halt_callback_chains_on_return_false = false
I'm guessing this has something to do with it?
Edit: The factory code is below. This exact code was working 100% before upgrading the gems:
FactoryBot.define do
factory :book do
name "my songbook"
end
end
FactoryBot changed its syntax. It now expects a block when assigning values – even static values – to variables. Change your code to this:
FactoryBot.define do
factory :book do
name { "my songbook" }
end
end
See Defining Factories in their docs.

WickedPdf and Rails 5.1.4: can't instantiate

I want to generate a PDF in a ruby service used by a Rails 5.1.4 app. It fails kind of quickly.
class PdfRenderer
require 'wicked_pdf'
def self.generate
WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>')
end
end
When I call PdfRenderer.generate in my rspec tests, I get that error:
NoMethodError Exception: undefined method `pdf_from_string' for nil:NilClass
I can't instantiate the WickedPdf class. When I call WickedPdf.new in byebug, I get nil.
The stacktrace:
NoMethodError:
undefined method `pdf_from_string' for nil:NilClass
# ./app/services/pdf_renderer.rb:25:in `generate'
# ./spec/services/pdf_renderer_spec.rb:40:in `block (2 levels) in <top (required)>'
# /usr/local/bundle/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
# /usr/local/bundle/gems/spring-2.0.2/lib/spring/command_wrapper.rb:38:in `call'
# /usr/local/bundle/gems/spring-2.0.2/lib/spring/application.rb:201:in `block in serve'
# /usr/local/bundle/gems/spring-2.0.2/lib/spring/application.rb:171:in `fork'
# /usr/local/bundle/gems/spring-2.0.2/lib/spring/application.rb:171:in `serve'
# /usr/local/bundle/gems/spring-2.0.2/lib/spring/application.rb:141:in `block in run'
# /usr/local/bundle/gems/spring-2.0.2/lib/spring/application.rb:135:in `loop'
# /usr/local/bundle/gems/spring-2.0.2/lib/spring/application.rb:135:in `run'
# /usr/local/bundle/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in `<top (required)>'
# -e:1:in `<main>'

Some models raise ArgumentError: Unknown validator, others finds the validator

I have encountered a strange error with a custom attribute validator. I had the following setup and everything worked as expected (every class is wrapped with module API and V1):
# app/models/api/v1/business_account.rb
class BusinessAccount
has_many :transactions, class_name: BusinessAccountThings::Transaction
end
# app/models/api/v1/business_account_things/transaction.rb
module BusinessAccountThings
class Transaction
belongs_to :business_account
validates :amount, money: true
end
end
# app/validators/api/v1/money_validator.rb
class MoneyValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless /^\d+\.*\d{0,2}$/ =~ value.to_s
record.errors[attribute] << 'Only two digits are allowed.'
end
end
end
I now want to use the custom validator in BusinessAccount, like this:
class BusinessAccount
has_many :transactions
validates :current_balance, money: true
end
I then get the following error when running the spec for BusinessAccount (and a similar error when trying to load the class from Rails console):
/home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:120:in `rescue in block in validates': Unknown validator: 'MoneyValidator' (ArgumentError)
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:117:in `block in validates'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:113:in `each'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:113:in `validates'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:22:in `<class:Transaction>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:5:in `<module:BusinessAccountThings>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:3:in `<module:V1>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:2:in `<module:API>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:1:in `<top (required)>'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:360:in `require_or_load'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:494:in `load_missing_constant'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:16:in `<class:BusinessAccount>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:4:in `<module:V1>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:2:in `<module:API>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:1:in `<top (required)>'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:360:in `require_or_load'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:494:in `load_missing_constant'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing'
from /home/hannes/workspaces/rails-app/spec/models/api/v1/business_account_spec.rb:5:in `<top (required)>'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `load'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `each'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:106:in `setup'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:92:in `run'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
from /home/hannes/.rbenv/versions/2.2.5/bin/rspec:23:in `load'
from /home/hannes/.rbenv/versions/2.2.5/bin/rspec:23:in `<main>'
Line 16 in BusinessAccount is has_many :transactions, class_name: BusinessAccountThings::Transaction, dependent: :destroy
Line 22 in Transaction is validates :amount, money: true.
If I remove Line 22 from Transaction, the problem disappears.
If I run my spec for Transaction, I get this error:
1) API::V1::BusinessAccountThings::Transaction
Failure/Error:
validates :current_balance,
numericality: { greater_than_or_equal_to: 0 },
money: true
ArgumentError:
Unknown validator: 'MoneyValidator'
# ./app/models/api/v1/business_account.rb:9:in `<class:BusinessAccount>'
# ./app/models/api/v1/business_account.rb:4:in `<module:V1>'
# ./app/models/api/v1/business_account.rb:2:in `<module:API>'
# ./app/models/api/v1/business_account.rb:1:in `<top (required)>'
# ./spec/models/api/v1/business_account_things/transaction_spec.rb:5:in `block (2 levels) in <top (required)>'
# ./spec/models/api/v1/business_account_things/transaction_spec.rb:7:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# NameError:
# uninitialized constant API::V1::BusinessAccount::MoneyValidator
# ./app/models/api/v1/business_account.rb:9:in `<class:BusinessAccount>'
So it appears as if both classes cannot load the MoneyValidator at the same time... How can I solve this?
Autoloading API::V1::MoneyValidator fails with your setup.
business_account.rb is trying to load it as API::V1::BusinessAccount::MoneyValidator. That fails because it is namespaced under API::V1, not API::V1::BusinessAccount.
Autoloading will also try to find MoneyValidator (without namespace; as written in the source code). So if you move your validator class out of the API::V1 namespace and make it a top-level constant, then it should work.

NoMethodError: undefined method `key_columns' for #<Class:0x00000007863dd0>

I'm new at rails and also cequel. I have existing model witch was genearted on Postgres and now i'm trying to migrate it to the Cassandra.
1. I would just like to migrate one of my model and other i would like to leave on Postgres. Is this possible?
My models:
class TripPoint
include Cequel::Record
belongs_to :trip
key :device_created_at, :timestamp
key :trip_points_id, :timeuuid, auto: true
column :rssi, :int
....
end
class Trip < ActiveRecord::Base
has_many :trip_points
end
When i try to run: rake cequel:migrate
I'm getting the error:
rake aborted!
NoMethodError: undefined method `key_columns' for #<Class:0x00000007863dd0>
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.10/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/cequel-1.7.0/lib/cequel/record/associations.rb:117:in `belongs_to'
/home/zmagajna/Ruby/ULU/ulu-rails/app/models/trip_point.rb:10:in `<class:TripPoint>'
/home/zmagajna/Ruby/ULU/ulu-rails/app/models/trip_point.rb:1:in `<top (required)>'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.10/lib/active_support/dependencies.rb:443:in `load'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.10/lib/active_support/dependencies.rb:443:in `block in load_file'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.10/lib/active_support/dependencies.rb:633:in `new_constants_in'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.10/lib/active_support/dependencies.rb:442:in `load_file'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.10/lib/active_support/dependencies.rb:342:in `require_or_load'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.10/lib/active_support/dependencies.rb:307:in `depend_on'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.10/lib/active_support/dependencies.rb:225:in `require_dependency'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/cequel-1.7.0/lib/cequel/record/tasks.rb:61:in `block in migrate'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/cequel-1.7.0/lib/cequel/record/tasks.rb:55:in `each'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/cequel-1.7.0/lib/cequel/record/tasks.rb:55:in `migrate'
/home/zmagajna/.rvm/gems/ruby-2.1.2/gems/cequel-1.7.0/lib/cequel/record/tasks.rb:21:in `block (2 levels) in <top (required)>'
/home/zmagajna/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/home/zmagajna/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => cequel:migrate
(See full trace by running task with --trace)
My environment:
ruby 2.1.2p95
Rails 4.1.10
cequel (= 1.7.0)

Getting undefined method `to_sym' from FactoryGirl create

I have a simple spec:
describe Setting do
subject { create(:setting) }
it { should be_valid }
end
with the factory:
FactoryGirl.define do
factory :setting do
key "some_key"
value "The appropriate value"
end
end
When I run the spec:
rake spec SPEC=spec/models/setting_spec.rb
I get the following error:
1) Setting should be valid
Failure/Error: it { should be_valid }
NoMethodError:
undefined method `to_sym' for nil:NilClass
# ./spec/models/setting_spec.rb:6:in `block (2 levels) in <top (required)>'
I tried using a let(:setting) in place of the subject line and got the same error.
EDIT Per comments:
The error refers to the line it { should be_valid }
The model is:
class Setting < ActiveRecord::Base
attr_accessor :key, :value
validates_uniqueness_of :key
end
Here's the trace:
# /var/lib/gems/1.9.1/gems/activerecord-3.2.16/lib/active_record/validations/uniqueness.rb:26:in `validate_each'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validator.rb:153:in `block in validate'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validator.rb:150:in `each'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validator.rb:150:in `validate'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:310:in `_callback_before_1986'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:407:in `_run__3464288744618424855__validate__1974049388273351148__callbacks'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:405:in `__run_callback'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validations.rb:228:in `run_validations!'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:403:in `_run__3464288744618424855__validation__1974049388273351148__callbacks'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:405:in `__run_callback'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
# /var/lib/gems/1.9.1/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
# /var/lib/gems/1.9.1/gems/activemodel-3.2.16/lib/active_model/validations.rb:195:in `valid?'
# /var/lib/gems/1.9.1/gems/activerecord-3.2.16/lib/active_record/validations.rb:69:in `valid?'
# /var/lib/gems/1.9.1/gems/rspec-expectations-2.14.5/lib/rspec/expectations/handler.rb:24:in `handle_matcher'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/memoized_helpers.rb:68:in `should'
# ./spec/models/setting_spec.rb:6:in `block (2 levels) in <top (required)>'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:114:in `instance_eval'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:114:in `block in run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:254:in `with_around_each_hooks'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:111:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:390:in `block in run_examples'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:386:in `map'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:386:in `run_examples'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:371:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:28:in `map'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:28:in `block in run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/reporter.rb:58:in `report'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:25:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:77:in `rescue in run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:73:in `run'
# /var/lib/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'
spec_helper.rb: http://pastebin.com/6fzDXnux
The direct cause for this error is that the class derives from ActiveRecord::Base, and ActiveRecord can't determine the primary key (required by AR for validating uniqueness). Most likely, this is happening either because you haven't run migrations to create the settings table, or you created the settings table without a primary key.
The offending line (based on the backtrace provided):
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
Note that to_sym is being called on finder_class.primary_key.
you can also solve same by adding below line in your model.
self.primary_key = 'id'
e.g.
Class Person
self.primary_key = 'id'
end

Resources