ActiveRecord::Relation.concat failing in Rails 5 - ruby-on-rails

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.

Related

Is there an updated version of 'where_clauses' method for Arel in Rails?

So I updated Rails from 4.0.1 to 4.1.16 and I'm getting errors along the lines of:
Failure/Error: self.published.arel.where_clauses.join(' AND ')
NoMethodError:
undefined method `where_clauses' for #<Arel::SelectManager:0x007fd773b1dd50>
I looked through the documentation for Arel (using version 5.0.1.20140414130214) and I can't find the same method. Has it been replaced and if so, with what?
Has it been replaced and if so, with what?
It was deprecated and then removed with no replacement. Seems that you need to review the logic of the places, which used where_clauses and reimplement those parts of the application using the current existing methods of arel

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)

Unable to use active record time methods inside ruby on rails 5.0 model

I'm currently unable to use any of the active support time methods inside my ruby on rails 5.0 model like the following:
5.seconds
2.days
10.minutes
throws an error:
NoMethodError: undefined method `seconds' for AS::Duration:0x007f97a5903b90 #value=5, #parts=[[:seconds, 5]] Did you mean? send
EDIT: here is the actual code causing an issue.
ReminderJob.set(wait: 5.seconds).perform_later(self.user.id)
Even tho I can see people using the below code fine and it works
UserReminderJob.set(wait: 1.week).perform_later user
However, it works in my console and in my controllers and views.
The error message states that the object is AS::Duration:0x007f97a5903b90, NOT an integer -- therefore the example of 5.seconds will not reproduce the problem.
This is also unusual, since 5.seconds will normally return an ActiveSupport::Duration object, not AS::Duration.
I would therefore hazard a guess that you're actually using the as-duration ruby gem rather than built-in rails behaviour. This extends the the Integer class in a different way, and returns an object that doesn't behave like an integer.
I think that an actual reproduction of your error could be achieved with: 5.seconds.seconds. In standard rails, this works fine (and returns the same value as 5.seconds), since ActiveSupport::Duration instances behave like Integers. But with this gem, it fails with the above error.

rails dot notation no longer works for accessing hash values gives NoMethodError

I'm using rails 5 with ruby 2.3.3 . Today I added a gem, there was a version conflict so I took the gem out. Since then dot notation such as hash.test no longer works. It gives NoMethodError: private method test called for {:test=>"value"}:Hash
How can I access hashes with dot notation again?
Whatever you're using to use dot-notation to access a hash is probably using method_missing to trap your dot-notation method calls. But everything has a test method because Kernel#test exists and everything includes Kernel; also, pretty much everything in Kernel is private because Kernel is where methods go that we want to pretend are functions. For example:
> 'pancakes'.test
NoMethodError: private method `test' called for "pancakes":String
I suspect that you problem is your choice of :test as hash key.

Rails 4 : undefined method `increment!'

I am trying to fix a Redmine plugin written for Redmine 2.5 to work with Redmine 3 which uses Rails 4. When it executes the following code:
project_issue_key.increment!(:last_issue_number)
I get the following exception:
NoMethodError (undefined method `increment!' for #<ProjectIssueKey::ActiveRecord_Relation:0x007f65f52cf648>):
I am thinking that the increment! method has been deprecated. How is this done in Rails 4?
You should call increment! on the object, not ActiveRecord::Relation. Something like ProjectIssueKey.first.increment!(:last_issue_number).
increment! is still present in Rails 4 , check these docs.

Resources