Rails - undefined method 'reflections' exception - ruby-on-rails

Calling really_destroy! on an active record in order to hard delete the record throws the following exception. How can I resolve this?
https://github.com/radar/paranoia
myRecord.really_destroy!
NoMethodError: undefined method 'reflections' for #MyActiveRecord:0x00000102793aa8
from /Users/name/.rvm/gems/ruby-2.0.0-p451#company/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:435:in `method_missing'

This was a bug in Paranoia library which is fixed on the latest version. It was due to a change in ActiveModel where reflections method is no longer an instance method, rather it's now a class method

Related

ActiveRecord::Relation.concat failing in Rails 5

Stupid question but I'm not sure why this would work in Rails 4.2 but not in Rails 5.2.
FamilyCharacteristic.where(family_id: #user.family_ids)
.concat(#user.characteristics)
Specs fail in 5.2:
Failure/Error:
FamilyCharacteristic.where(family_id: #user.family_ids)
.concat(#user.characteristics)
NoMethodError:
undefined method `concat' for #<ActiveRecord::Relation []>
Did you mean? count
Was concat removed from ActiveRecord::Relation in 5.2 or was FamilyCharacteristic.where(family_id: #user.family_ids) somehow a different object in < 4.2?
Thanks for any help.
I did some digging and found out that:
FamilyCharacteristic.where(family_id: #user.family_ids)'s class didn't change, it's still ActiveRecord::Relation
Relation didn't and still doesn't define its own concat method, but it was delegated to Array#concat until this commit happened, so in Rails 4.2 SomeModel.where(id: ids).concat(some_records)(which returns an Array) was actually the same as SomeModel.where(id: ids).to_a.concat(some_models)
After mentioned before change in ActiveRecord::Delegation, in Rails 5.2, the only methods delegated to Array are the ones specified in this module and concat is not among them
To sum up - concat from your example was never part of ActiveRecord but was delegated to Array#concat and that's why it worked. It's no longer delegated in Rails 5 so it throws NoMethodError.

Rspec - Check for the instance of ActiveRecord::Relation of a specific Model

In our application, we have used this
expect_any_instance_of(Order::ActiveRecord_Relation)
.to receive(:something)
As we upgraded the application to rails 5.2 we are getting the following error
NameError:
private constant #<Class:0x000055aa351fc9a0>::ActiveRecord_Relation referenced
Is there a way to check for ActiveRecord::Relation of a specific Model with expect_any_instance_of
There a issue raised for the same https://github.com/rails/rails/issues/30943
try this Order.const_get(:ActiveRecord_Relation). it should make it work in rails >= 5.2
expect_any_instance_of(Order.const_get(:ActiveRecord_Relation))
.to receive(:something)

rails_admin: undefined method `year' for nil:NilClass

Using rails_admin, I whenever I try to save an object that has a date field, I get the error message: undefined method 'year' for nil:NilClass. Not sure what's going on.
Turns out it's a bug that's introduced with Ruby 2.2.*
Not sure if it's on the rails_admin team's radar yet, but you can fix it by downgrading to 2.1.*

.to_date method failing

I am using rails 3.2.16 and ruby 1.9.3
I get this error from the localhost browser after rails s.
undefined method `to_date' for nil:NilClass
like this
NoMethodError in Products#new
Showing /Users/main/railscasts-episodes/episode-350/store-
after/app/views/products/_form.html.erb where line #27 raised:
undefined method `to_date' for nil:NilClass
in irb> mode it works only when I
require 'date'
Now my question is about my rails project. In which file in my project I should add
require 'date'
I added it into my model it did not work. Or, maybe I need to install any specific gem? to make this work
The problem you have isn't an issue with to_date, it's a problem with the object you're trying to perform the method on
Your issue will either be:
You have not declared the variable / object
Your variable / object is not populated with any data
I'd highly recommend posting the controller action you're using to invoke the to_date method. Obviously, there may be an issue with the way you're calling it, but the immediate issue is you're trying to deal with an unpopulated variable

undefined method page for #<Array:0xc347540> kaminari "page" error. rails_admin

i am using rails_admin. when i go to certain resource. by typin url
localhost:3000/admin/rule
than it give me this error. code is:
scope = Rule.all
scope.page(1).per(2)
. above code is writtten in rails_admin gem.in a file named mongoid.rb placed in adaptors folder. complete log is:
NoMethodError (undefined method `page' for #<Array:0xcea7408>):
mongoid (2.4.8) lib/mongoid/criteria.rb:385:in `method_missing'
/home/usman/.rvm/gems/ruby-1.9.2-p290#system/bundler/gems/kaminari-809105ad782a/lib/kaminari/models/mongoid_extension.rb:11:in `page'
/home/usman/.rvm/gems/ruby-1.9.2-p290#system/bundler/gems/rails_admin-069819944cc9/lib/rails_admin/adapters/mongoid.rb:37:in `all'
/home/usman/.rvm/gems/ruby-1.9.2-p290#system/bundler/gems/rails_admin-069819944cc9/app/controllers/rails_admin/main_controller.rb:127:in `get_collection'
/home/usman/.rvm/gems/ruby-1.9.2-p290#system/bundler/gems/rails_admin-069819944cc9/app/controllers/rails_admin/main_controller.rb:39:in `list_entries'
/home/usman/.rvm/gems/ruby-1.9.2-p290#system/bundler/gems/rails_admin-069819944cc9/lib/rails_admin/config/actions/index.rb:30:in `block (2 levels) in <class:Index>'
what should i do to resolve this error?
You can not call Kaminari methods on Array, because Rule.all will return Array.
So you have to do something like this: Rule.page(1).per(2)
Here is documentation and examples of Kaminari usage:
https://github.com/amatsuda/kaminari
I've been running into this issue off and on for a while now using Mongoid. Sometimes refreshing the page in RailsAdmin would fix it.
I figured out that the problem is Kaminari's hooks are not initialized in my environment, so the models that rely on Kaminari's extension methods don't have them available.
I simply took the following line from Kaminari's railtie and put it at the top of my rails_admin initializer:
Kaminari::Hooks.init
Now things seem to be working for me. However, I don't know why the ActiveSupport callback is not running that code.
Use this
Kaminari.paginate_array(Rule.all).page(params[:page])
Kader's solution is great! The only thing is I found I have to add .per to make it work.
Kaminari.paginate_array(Rule.all).page(params[:page]).per(PER_PAGE_RECORDS)

Resources