Factory Girl, issue with InvalidRecord error - ruby-on-rails

I have a problem with the error:
ActiveRecord::RecordInvalid
caused by this:
let(:ind2){ build(:ind2) }
Rspec test:
describe '#client_free_time_validation' do
let(:ind) { build(:ind).tap {|e| p e.valid?; p e.errors}}
let(:ind2){ build(:ind2).tap {|e| p e.valid?; p e.errors} }
context 'when training is during another training' do
it 'raises an error' do
expect(ind.valid?).to be_truthy
expect(ind2.valid?).to be_falsey
# expect(ind2.errors.count).to eq 1
# expect(ind2.errors[:base]).to eq(['Masz w tym czasie inny trening.'])
end
end
Factory:
FactoryGirl.define do
factory :individual_training do
date_of_training { Date.today.next_week.advance(days: 1) }
association :client, factory: :client
association :trainer, factory: :trainer
start_on Time.parse('12:30')
end_on Time.parse('13:30')
association :training_cost, factory: :tc2
factory :ind do
start_on Time.parse('11:00')
end_on Time.parse('12:00')
end
factory :ind2 do
start_on Time.parse('10:30')
end_on Time.parse('11:30')
end
end
end
I'm confused because similar let working in another test. I tried debugging by using tap method but it doesn't show error messages(in another case nice presents).
If I should put some additional data(how my model looks like etc.), please write.
I saw that if I comment the first let or second, the test passes. It looks at situations like two let(ind and ind2) couldn't work together.
Example values of attributes generate by test:
#<ActiveModel::Errors:0x00000001f4ade8 #base=#<IndividualTraining id: nil, date_of_training: "2016-08-23", client_id: 28, trainer_id: 29, start_on: "2016-08-20 11:00:00", end_on: "2016-08-20 12:00:00", training_cost_id: 4>, #messages={}>
Do you have a suggestion how to debug what record is invalid?
Update:
Full error message:
Failure/Error: let(:ind2){ build(:ind2).tap {|e| p e.valid?; p e.errors} }
ActiveRecord::RecordInvalid:
Nieprawidłowy rekord
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5.1/lib/active_record/validations.rb:79:in `raise_record_invalid'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5.1/lib/active_record/validations.rb:43:in `save!'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5.1/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5.1/lib/active_record/transactions.rb:291:in `block in save!'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5.1/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5.1/lib/active_record/transactions.rb:220:in `transaction'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5.1/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5.1/lib/active_record/transactions.rb:291:in `save!'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/configuration.rb:18:in `block in initialize'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/evaluation.rb:15:in `create'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy/create.rb:9:in `tap'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy/create.rb:9:in `result'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory.rb:42:in `run'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.5.1/lib/active_support/notifications.rb:166:in `instrument'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:28:in `run'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy/build.rb:5:in `association'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/evaluator.rb:31:in `association'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute/association.rb:19:in `block in to_proc'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/evaluator.rb:75:in `instance_exec'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/evaluator.rb:75:in `block in define_attribute'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:56:in `get'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/evaluation.rb:12:in `object'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy/build.rb:9:in `result'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory.rb:42:in `run'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.5.1/lib/active_support/notifications.rb:166:in `instrument'
# /home/lukas/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:28:in `run'
# /home/lukas/.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'
# ./spec/models/individual_training_spec.rb:60:in `block (3 levels) in <top (required)>'
# ./spec/models/individual_training_spec.rb:65:in `block (4 levels) in <top (required)>'
Update 2:
The error disappears when I set date_of_training Date.today in a factory. I also tried to set manually date like 2016-08-23, but it's still InvalidRecord. Also Date.today + 1.day not working. Any idea what can be wrong? Maybe date_of_training isn't a direct problem.
Update 3:
IndividualTraining model validations:
private
def date_and_start_on_validation
unless start_on.blank?
if date_of_training < Date.today
errors.add(:base, 'You cannot set individual training before today.')
elsif date_of_training == Date.today
if start_on <= Time.now
errors.add(:base, 'Time of today training is before current time.')
end
end
end
end
# check if trainer work while will be individual_training
def date_of_training_validation
unless start_on.blank?
trainer.work_schedules.each_with_index do |ti, ind|
if ti.day_of_week == BackendController.helpers.translate_date(date_of_training)
if (start_on.strftime('%H:%M')..end_on.strftime('%H:%M'))
.overlaps?(ti.start_time.strftime('%H:%M')..ti.end_time.strftime('%H:%M'))
break
else
errors.add(:base, 'Training is outside of trainer work schedule.')
end
elsif ind == trainer.work_schedules.size - 1
errors.add(:base, 'In this day trainer doesn't work.')
end
end
end
end
# check if client doesn't have another training or activity
def client_free_time_validation
unless start_on.blank?
client.individual_trainings_as_client.where(date_of_training: date_of_training)
.where('id != ?', id).each do |ci|
if (start_on...end_on).overlaps?(ci.start_on...ci.end_on)
errors.add(:base, 'You have another training.')
end
end
client.activities.where(day_of_week: BackendController.helpers.translate_date(date_of_training))
.each do |ca|
if (start_on...end_on).overlaps?(ca.start_on...end_on)
errors.add(:base, 'You have another activity.')
end
end
end
end
Update 4:
I noticed that if I first execute ind.valid? - it will be true and ind2 will be RecordInvalid. But when I reload and check ind2.valid? - now it is true and ind false.
Update 5:
I used the same let but separately in a different context, and rspec passed. What could be the reason, that I cannot use two let in the same context?
IndividualTraining associations
belongs_to :trainer, class_name: 'Person', foreign_key: 'trainer_id'
belongs_to :client, class_name: 'Person', foreign_key: 'client_id'
belongs_to :training_cost
client and trainer factories
FactoryGirl.define do
factory :person do
pesel { Faker::Number.number(11) }
first_name 'Thomas'
last_name 'Owel'
date_of_birth { Faker::Time.between('1970-01-01', '2000-12-31') }
email { Faker::Internet.email }
password { Faker::Internet.password }
type 'Person'
end
factory :client, parent: :person, class: 'Client' do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
type 'Client'
end
factory :trainer, parent: :person, class: 'Trainer' do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
type 'Trainer'
salary { Faker::Number.decimal(4, 2) }
hiredate { Faker::Time.between('2016-01-01', '2016-04-30') }
end
end
Update 6:
In another context I have this let:
let(:individual_training) { build :individual_training, trainer_id: work_schedule[:person_id] }
let(:ind2) do
build :individual_training,
trainer_id: work_schedule[:person_id],
date_of_training: Date.today.next_week.advance(days: 0),
start_on: Time.now - 1.hour,
end_on: Time.now
end
This nice works. There is no error: RecordInvalid

These could be the reason behind "Invalid record error" -
Reason - It usually happens when you use "create" and "build" together.
create method persists the instance while the build method keeps it only in memory.
Firstly, I will suggest you to use build method and please check validations related to it. There is something wrong in your validations.

Related

ActiveStorage::IntegrityError Rspec attaching file

I have a factory for create dummy data, the factory name is migration_session.
So i create one instance of that factory by calling like this
#loan_migration = create(:migration_session, :loan, cooperative: #cooperative, management: #manager)
and this is my migration_session factory code
FactoryBot.define do
factory :migration_session, class: Migration::Session do
cooperative
management
trait :loan do
excel_file { Rack::Test::UploadedFile.new("#{Rails.root}/public/payment_migration.xlsx", 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') }
end
this is my migration_session model
class Migration::Session < ApplicationRecord
self.table_name = "migration_sessions"
has_one_attached :excel_file
end
because i call trait loan, it will attached excel_file attributes by excel file payment_migration.xlsx. but error like this comes after try to save the instance
ActiveStorage::IntegrityError:
ActiveStorage::IntegrityError
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:154:in `ensure_integrity_of'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:21:in `block in upload'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service.rb:126:in `instrument'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:19:in `upload'
# /usr/local/bundle/gems/activestorage-6.0.3.4/app/models/active_storage/blob.rb:196:in `upload_without_unfurling'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/attached/changes/create_one.rb:25:in `upload'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/attached/model.rb:56:in `block in has_one_attached'
# ./spec/requests/bulk_uploads_spec.rb:36:in `block (2 levels) in <top (required)>'
# /usr/local/bundle/gems/webmock-3.9.5/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'
How to solve this problem? My file just fine, i can open and edit it.
I thing your problem is in the file path
excel_file { Rack::Test::UploadedFile.new("#{Rails.root}/public/payment_migration.xlsx", 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') }
Depending on your SO, the '/' char may not be allowed on path, so instead of pass the path string, let rails fill it for you.
Use the Rails.root.join method, like this:
Rails.root.join('public', 'payment_migration.xlsx')

Rails Rspec - How to set polymorphic has_many association

I have a model (payment) that belongs to another model (event), via a polymorphic association.
Some tests are failing because the owner model (event) is accessed by the payment model in validations, but the event is returning nil. All the features work fine when testing app directly in the browser.
I added some more comments to payment.rb below.
I've tried defining the association in the factories, but no luck.
What is the best way to set up this association in the spec?
# models/event.rb
class Event < ApplicationRecord
has_many :payments, as: :payable, dependent: :destroy
end
# models/payment.rb
class Payment < ApplicationRecord
belongs_to :payable, polymorphic: true
validate :amount_is_valid
def amount_is_valid
if amount.to_i > payable.balance.to_i
errors.add(:amount, "can't be higher than balance")
end
end
end
Both examples in this spec are failing.
# spec/models/payment_spec.rb
require 'rails_helper'
RSpec.describe Payment, type: :model do
let!(:event) { FactoryBot.create(:event, event_type: 'test', total: 10000, balance: 10000) }
let!(:user) {FactoryBot.create(:user)}
let!(:payment) {
FactoryBot.build(:payment,
amount: 300,
method: 'cash',
payer_id: user.id,
payable_id: event.id,
status: 1,
)
}
describe 'Association' do
it do
# This will fail with or without this line
payment.payable = event
is_expected.to belong_to(:payable)
end
end
# Validation
describe 'Validation' do
describe '#amount_is_valid' do
it 'not charge more than event balance' do
# This will make the test pass. The actual spec has a lot more examples though,
# would rather just set the association once.
# payment.payable = event
payment.amount = 5000000
payment.validate
expect(payment.errors[:amount]).to include("can't be higher than balance")
end
end
end
end
Output
# bundle exec rspec spec/models/payment_spec.rb
Randomized with seed 42748
Payment
Association
should belong to payable required: true (FAILED - 1)
Validation
#amount_is_valid
not charge more than event balance (FAILED - 2)
Failures:
1) Payment Association should belong to payable required: true
Failure/Error: if amount.to_i > payable.balance.to_i
NoMethodError:
undefined method `balance' for nil:NilClass
# ./app/models/payment.rb:9:in `amount_is_valid'
# ./spec/models/payment_spec.rb:23:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:80:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:79:in `block (2 levels) in <top (required)>'
# ./spec/spec_helper.rb:108:in `block (2 levels) in <top (required)>'
2) Payment Validation #amount_is_valid not charge more than event balance
Failure/Error: if amount.to_i > payable.balance.to_i
NoMethodError:
undefined method `balance' for nil:NilClass
# ./app/models/payment.rb:9:in `amount_is_valid'
# ./spec/models/payment_spec.rb:39:in `block (4 levels) in <top (required)>'
# ./spec/rails_helper.rb:80:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:79:in `block (2 levels) in <top (required)>'
# ./spec/spec_helper.rb:108:in `block (2 levels) in <top (required)>'
Top 2 slowest examples (0.29972 seconds, 71.6% of total time):
Payment Association should belong to payable required: true
0.28796 seconds ./spec/models/payment_spec.rb:18
Payment Validation #amount_is_valid not charge more than event balance
0.01176 seconds ./spec/models/payment_spec.rb:32
Finished in 0.4186 seconds (files took 4.31 seconds to load)
2 examples, 2 failures
Failed examples:
rspec ./spec/models/payment_spec.rb:18 # Payment Association should belong to payable required: true
rspec ./spec/models/payment_spec.rb:32 # Payment Validation #amount_is_valid not charge more than event balance
Update
Passing specs based on Schwern's feedback.
Still using a custom validation for amount, because balance is a field on the associated payable, not the payment (couldn't find a way to access an associated model from inside a built-in validation helper)
# payment.rb
class Payment < ApplicationRecord
belongs_to :payable, polymorphic: true
validates :payable, presence: true
validate :amount_is_valid
def amount_is_valid
if amount > payable.balance
errors.add(:amount, "can't be greater than balance")
end
end
end
# spec/models/payment_spec.rb
require 'rails_helper'
RSpec.describe Payment, type: :model do
let(:event) { FactoryBot.create(:event, event_type: 'test', total: 10000, balance: 10000) }
let(:user) {FactoryBot.create(:user)}
let(:payment) {
FactoryBot.build(:payment,
amount: 300,
method: 'cash',
payer_id: user.id,
payable: event,
status: 1,
)
}
describe '#payable' do
it 'is an Event' do
expect(payment.payable).to be_a(Event)
end
end
describe '#amount' do
context 'amount is higher than balance' do
before {
payment.amount = payment.payable.balance + 1
}
it 'is invalid' do
payment.validate
expect(payment.errors[:amount]).to include("can't be greater than balance")
end
end
end
end
Your first test is not failing where you think it is. It's failing on the next line, is_expected.to belong_to(:payable).
You're setting payment, but you're testing the implicitly defined subject which will be Payment.new.
is_expected.to belong_to(:payable)
Is equivalent to...
expect(subject).to belong_to(:payable)
And since you have no defined subject this is...
expect(Payment.new).to belong_to(:payable)
Payment.new does not have payable defined and so the amount_is_valid validation errors.
To fix this, test payment directly. And I would suggest staying away from subject while you're learning RSpec. And you should not have to set payment.event, it's already set in the factory.
describe 'Association' do
expect(payment).to belong_to(:payable)
end
But I'm not aware of a belong_to matcher. You should not be directly checking implementation, but rather its behavior. The behavior you want is for payment.payable to return a Payable.
describe '#payable' do
it 'is a Payable' do
expect(payment.payable).to be_a(Payable)
end
end
The second failure is because you have incorrectly initialized your Payment. You're passing in payable_id: event.id but that does not set payable_type. Without payable_type it doesn't know what class the ID is for.
Instead, pass the objects in directly.
let!(:payment) {
FactoryBot.build(:payment,
amount: 300,
method: 'cash',
payer: user,
payable: event,
status: 1,
)
}
Some more general cleanups...
let! will always run the block whether it's used or not. Unless you specifically need that, use let and the blocks will run as needed.
You expect payable to exist, so validate the presence of payable.
Use the built in numericality validator on amount.
class Payment < ApplicationRecord
belongs_to :payable, polymorphic: true
validates :payable, presence: true
validates :amount, numericality: {
less_than_or_equal_to: balance,
message: "must be less than or equal to the balance of #{balance}"
}
end
require 'rails_helper'
RSpec.describe Payment, type: :model do
let(:event) {
create(:event, event_type: 'test', total: 10000, balance: 10000)
}
let(:user) { create(:user) }
let(:payment) {
build(:payment,
amount: 300,
method: 'cash',
payer: user,
payable: event,
status: 1
)
}
# It's useful to organize tests by method.
describe '#payable' do
it 'is a Payable' do
expect(payment.payable).to be_a(Payable)
end
end
describe '#amount' do
# Contexts also help organize and name your tests.
context 'when the amount is higher than the payable balance' do
# This code will run before each example.
before {
# Rather than hard coding numbers, make your tests relative.
# If event.balance changes the test will still work.
payment.amount = payment.payable.balance + 1
}
it 'is invalid' do
expect(payment.valid?).to be false
expect(payment.errors[:amount]).to include("must be less than or equal to")
end
end
end
end

How to dynamic create models with RoR and mongodb?

I'm trying to create a dynamic application to create any kind of data needed through metaprogramming the MVC i tried this for models:
class DynamicRecord
attr_accessor :name, :attributes
def initialize(name, attributes = [])
raise "Error: Constant #{name} already in namespace" if name.in? Object.constants
a_new_class = Class.new(Object) do |clazz|
include Mongoid::Document
attributes.map do |attribute|
field attribute[:name], type: attribute[:type]
end
end
Object.const_set(name, a_new_class)
end
end
DynamicRecord.new('Person', [{name: :name, type: String}, {name: :email, type: String}])
person = Person.new(name: "Foo", email: "Foo#foo.com")
person.save
Then I get this error:
Mongo::Error::OperationFailure: Invalid ns [mongodb_divcad_development.] (16257)
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/result.rb:256:in `validate!'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/write/insert.rb:60:in `block in execute_message'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/server/connection_pool.rb:107:in `with_connection'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/server.rb:242:in `with_connection'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/write/insert.rb:59:in `execute_message'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/write/write_command_enabled.rb:39:in `execute'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/collection.rb:365:in `block in insert_one'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/retryable.rb:112:in `write_with_retry'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/collection.rb:356:in `insert_one'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/query_cache.rb:182:in `insert_one_with_clear_cache'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:79:in `insert_as_root'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:27:in `block in insert'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:118:in `block (2 levels) in prepare_insert'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:373:in `_run__4510143668266298615__create__callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/interceptable.rb:138:in `run_callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:117:in `block in prepare_insert'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:373:in `_run__4510143668266298615__save__callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/interceptable.rb:138:in `run_callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:116:in `prepare_insert'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:23:in `insert'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/savable.rb:23:in `save'
from (irb):70
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
This can be done? There is a better approach without falling into a Entity-attribute-value model?
Mongoid doesn't know about namespace when you try to use dynamic model. In order to fix it you should set namespace with store_in
Example:
class DynamicCollection
def self.create(collection, fields)
klass = Class.new do
include Mongoid::Document
store_in collection: collection.downcase
fields.each do |item|
field item[:name], type: item[:type]
end
end
Object.const_set(collection, klass)
end
end
fields = [
{name: 'name', type: String},
{name: 'email', type: String}
]
DynamicCollection.create('Demo', fields)
Demo.create!(name: 'SomeValue', email: 'SomeValue')
I didn't try this locally, and this could be a dumb answer...but I would change
attributes.map to attributes.each seems like map would return an array and would cause an error.
Also...
https://github.com/mongodb/mongoid/blob/master/lib/mongoid/fields.rb#L335
Looking at the source for mongoid. There is a method called add_field that you maybe able to utilize.

`object.message if object.respond_to? :message` works, but is proving difficult to test

I have the following setup in a rails app:
class Song < ActiveRecord::Base
after_commit :update_singable_index
belongs_to :singable, polymorphic: true
def update_singable_index
singable.update_index if singable.respond_to? :update_index
end
end
I've redacted the actual class heavily. Please let me know if any more information is needed.
But basically the Song model has a polymorphic association with a bunch of models. Some of them have elasticsearch indices and some of them don't. As a result some of them will accept the update_index message, while others will throw an NoMethodError: undefined method 'update_index'
The sing factory looks like this:
FactoryGirl.define do
factory :song do
album { create(:album) }
end
trait :for_updatable do
updatable_type 'Updatable'
association :singable, factory: :updatable
end
trait :for_unupdatable do
updatable_type 'Unupdatable'
association :singable, factory: :unupdatable
end
end
Then in the test I have the following setup:
RSpec.describe Song, type: :model do
describe '.update_index_of_updatable' do
it 'updates Updatable\'s index' do
song = create(:song, :for_updatable)
updatable = song.singable
expect(updatable).to receive(:update_index)
song.save
end
it 'doesn\'t attempt to update an Unupdatable\'s index' do
song = create(:song, :for_unupdatable)
unupdatable = song.singable
expect(unupdatable).not_to receive(:update_index)
song.save
end
end
end
The first test does indeed pass. The second test on the other hand fails
1) Song.update_singable_index doesn't attempt to update an Unupdatable's index
Failure/Error: update.save
(#<Unupdatable:0x0000000b39be98>).update_index(no args)
expected: 0 times with any arguments
received: 1 time
# ./app/models/song.rb:103:in `update_index_of_updatable'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:430:in `block in make_lambda'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:261:in `call'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:261:in `block in simple'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `call'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `block in call'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `each'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `call'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:88:in `run_callbacks'
# /usr/local/rvm/gems/ruby-2.2.3/gems/bugsnag-2.8.12/lib/bugsnag/rails/active_record_rescue.rb:8:in `run_callbacks'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:314:in `committed!'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:89:in `commit_records'
# /usr/local/rvm/gems/ruby-2.2.3/gems/test_after_commit-0.4.1/lib/test_after_commit.rb:47:in `test_commit_records'
# /usr/local/rvm/gems/ruby-2.2.3/gems/test_after_commit-0.4.1/lib/test_after_commit.rb:23:in `block in transaction_with_transactional_fixtures'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
# /usr/local/rvm/gems/ruby-2.2.3/gems/test_after_commit-0.4.1/lib/test_after_commit.rb:9:in `transaction_with_transactional_fixtures'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:220:in `transaction'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:286:in `block in save'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:285:in `save'
# ./spec/models/song_spec.rb:141:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:417:in `block (2 levels) in <top (required)>'
# ./spec/rails_helper.rb:192:in `block (2 levels) in <top (required)>'
# /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-retry-0.4.2/lib/rspec/retry.rb:52:in `block (3 levels) in apply'
# /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-retry-0.4.2/lib/rspec/retry.rb:43:in `times'
# /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-retry-0.4.2/lib/rspec/retry.rb:43:in `block (2 levels) in apply'
You didn't show your :for_unupdatable trait, but it must be the flawed part here (meaning, a singable of a song with this trait still responds to update_index).
So, you should fix that.
Alternatively, provide a dummy implementation of that method for non-ES-capable models.
class ElasticSearchSingable
# your regular update_index
end
class SimpleSingable
def update_index
# do nothing here,
# purpose of this empty method is conformance to common API
end
end

Getting Rspec error no implicit conversion of Symbol into Integer with Mongoid

I'm tying to test my Rails app with Rspec, but I'm getting a no implicit conversion of Symbol into Integer error without any apparent reason. Based on the traceback I get I think the problem is related to Mongo/Mongoid, however, I can't figure out what it is exactly. The code runs perfectly in production. The error happens only when testing.
Brief look at the model without the other methods:
class Card
include Mongoid::Document
field :front, type: String
field :back, type: String
field :level, type: Integer, default: 1
field :review_date, type: DateTime, default: DateTime.now
has_many :card_statistic, dependent: :destroy
belongs_to :topic
belongs_to :user
validates :front, :back, :level, presence: true
validates :topic, presence: { is: true, message: "must belong to a topic." }
validates :user, presence: { is: true, message: "must belong to a user." }
validates :level, numericality: { only_integer: true, greater_than: 0 }
end
One function in the model that triggers the error:
def self.reset(card)
card.update(level: 1)
end
The test code:
it "puts the given card in level 1" do
card = create(:card)
Card.correct card
card.reload
Card.correct card
card.reload
expect(card.level).to eq(3)
card.reset
card.reload
expect(card.level).to eq(1)
end
Then, the traceback of the error I get:
1) Card puts the given card in level 1
Failure/Error: Card.reset card
TypeError:
no implicit conversion of Symbol into Integer
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `[]'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `get'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/client.rb:170:in `read_preference'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:318:in `default_read'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:251:in `read'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/iterable.rb:38:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/query_cache.rb:207:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `block (2 levels) in first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:562:in `with_sorting'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:229:in `block in first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:474:in `try_cache'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:228:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual.rb:20:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/builders/referenced/in.rb:20:in `build'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:43:in `create_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:26:in `__build__'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:104:in `block (2 levels) in get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:130:in `_loading'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:100:in `block in get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:89:in `_building'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:99:in `get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:187:in `block in getter'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:79:in `read_attribute_for_validation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:149:in `block in validate'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `validate'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `public_send'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `block in make_lambda'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validate_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:395:in `run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `_run_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validation_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:334:in `valid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:97:in `valid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:371:in `invalid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:114:in `prepare_update'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:139:in `update_document'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/savable.rb:25:in `save'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:52:in `update'
# ./app/models/card.rb:57:in `reset'
# ./spec/models/card_spec.rb:32:in `block (2 levels) in <top (required)>'
The error is also triggered when testing the controllers. Even doing a get :index throws the error. Thanks in advance for your help.
Apparently, there where some issues with the very last builds of Mongoid and I was pulling the code directly from the repo without specifying a stable version. So the solution was to either downgrade the version to 4.0 or use the 5.0 beta build explicitly.
Either
gem 'mongoid', '~> 5.0.0.beta', github: 'mongoid/mongoid'
or
gem 'mongoid', '~> 4'
instead of
gem 'mongoid', '~> 5', github: 'mongoid/mongoid'

Resources