[Ruby / Rails] What to do when NoMethodError appears when using a destructive method such as filter!

The error that occurred

I got a NoMethodError when using a destructive method like filter !.

I've dealt with it, so I'll leave a note. (If you make a mistake, please kindly tell me ...)

How to reproduce

object_controller.rb


def index
  @object = Object.all
  @object.filter! do |o|
    o.id != 1
  end
end

When I got an object as an array with all, I could use a non-destructive method like filter, but I tried to make an array by playing an object that does not match the conditions with a destructive method like filter !.

However, as the very first heading says, I threw a NoMethodError.

How to deal with

object_controller.rb


def index
  @object = Object.all
  @object.to_a.filter! do |o|
    o.id != 1
  end
end

Converting an object to an array with to_a no longer throws a NoMethodError.

I will add it if I find out the reason.

I will leave a note on it so that I will not forget it. (Reminder)

If there are people who already know the reason and it seems that it is natural, I would be very happy if there is a gentleman who can put a link on something.

Recommended Posts

[Ruby / Rails] What to do when NoMethodError appears when using a destructive method such as filter!
What to do when Blocked Host: "host name" appears in Ruby on Rails
What to do when "call'Hoge.connection' to establish a connection" appears on rails c
What to do when rails creates a 〇〇 2.rb file
[Rails] What to do when the view collapses when a message is displayed with the errors method
What to do when a javax.el.PropertyNotWritableException occurs
What to do when undefined method ʻuser_signed_in?'
[Ruby] What to do when the error "cannot load such file" appears when executing VS Code debug
What to do when javax.el.ELException: Not a Valid Method Expression: appears when the JSF screen is displayed
What to do when a null byte error occurs
[Rails / Maybe it can be applied to other things? ] What to do if failure / error __send__ (method file) rspec cannnot road such file appears when executing RSpec
What to do when Method not found in f: ajax
What to do when Cannot format given Object as a Date in convertDateTime of JSF
[Ruby on Rails] Carousel of bootstrap4 is implemented as a slide show using each method.
What to do when a could not find driver appears when connecting to a DB in a Docker environment
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What to do if a SAX Parser error occurs when using Liferay 7 / DXP on AWS
What to do when Cannot apply expression operators to method binding
[Rails 6] What to do when a missing a template error occurs after introducing haml [Super easy]
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
I get a Ruby version error when I try to start Rails.
[Rails] What to do when rails s does not respond or does not stop
What to do when you become a Problem During Content Assist
What to do when you run into a docker-compose node_modules problem
What to do when javax.batch.operations.JobStartException occurs
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Rails] How to pass validation such as password when executing update action
Notes on what to do when a WebView ClassNotFoundException occurs in JavaFX 12
What to do if you get an "A server is already running." Error when you try to start the rails server
What to do if Command line is too long appears when building a gradle project in Intellij IDEA
[Beginner] What I learned when trying to introduce bootstrap to the rails6 app without using a CDN [Asset pipeline]
What to do when you want to delete a migration file that is "NO FILE"
<f: ajax> Unable to attach <f: ajax> to non-ClientBehaviorHolder What to do when you become a parent
What to do when rails db: seed does not reflect in the database
What to do when "Nil location provided. Can't build URI." Appears on CarrierWave
What to do when is invalid because it does not start with a'-'
What to do if ffi installation fails when launching an application in Rails
[Ubuntu] What to do when MongoDB cannot be started due to a SocketException error
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec
What to do if you get an error saying "Please enter a valid value" when getting with Rails datetime_field
What to do if you get an error saying "Could not find a JavaScript runtime." When starting rails server
[Rails] How to write when making a subquery
[Rails] How to create a graph using lazy_high_charts
[Ruby on Rails] NoMethodError undefined method `devise_for'error resolution
[Ruby] When adding a null constraint to a table
What to do when debugging "Source not found"
What do you use when converting to String?
What to do when IllegalStateException occurs in PlayFramework
[Ruby on Rails] How to use session method
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
What to do if you get a NoClassDefFoundError when trying to run eclipse on Java9
What to do when Address already in use is displayed after executing rails s
What to do if you get a port error when docker-compose up on Mac
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
What to do when an error (StandardError: An error has occurred, this and all later migrations canceled:) appears in rails db: migrate
<Dot installation> What to do if you cannot proceed due to an error when building a development environment for Rails learning.