How to push an object into ActiveRecord::Relation in rails 5

rails add to activerecord::relation
activerecord merge two queries
undefined method for activerecord associationrelation
rails add to collection
convert array to activerecord::relation
active record association proxy
rails association empty
rails association any

I am stuck in weird situtaion, I am working on upgrading an existing rails 3 appliction to rails 5 app.

Using rails 3, I have an object report which has many organizations

@report.organizations
#<ActiveRecord::Relation [#<Organization id: 1, name: "Org 1", description: nil, created_at: "2012-01-27", updated_at: "2019-01-15">]>
@report.organizations.count    # 1

when I push a new org object into existing activerecord relation it gives me new activerecord relation including new org

@report.organizations<< Organization.new
[#<Organization id: 1, name: "Org 1", description: nil, created_at: "2012-01-27", updated_at: "2019-01-15">, #<Organization id: nil, name: nil, description: nil, created_at: nil, updated_at: nil>]
# Getting count
@report.organizations.count    # 2

Using rails 5, I am getting exception

@report.organizations << Organization.new
*** NoMethodError Exception: undefined method `<<' for #<Organization::ActiveRecord_Relation:0x00007f93483e2640>

and when I do like

@report.organizations.to_a << Organization.new

it gives me

[#<Organization id: 1, name: "Org 1", description: nil, created_at: "2012-01-27", updated_at: "2019-01-15">, #<Organization id: nil, name: nil, description: nil, created_at: nil, updated_at: nil>]

but count is stil 1 instead of two

@report.organizations.count    # 1

Hope my issue is clear to you, please help me how to fix this issue. Thanks

According to the guide it should work. If it doesn't, try using create:

 @report.organizations.create

Cannot push an object into a collection · Issue #25906 · rails/rails , Try to push another instance of the model into that collection @hours << Hour.​new() undefined method '<<' for #<ActiveRecord::Relation []> See: https://​github.com/rails/rails/blob/master/activerecord/lib/active_record/relation.rb#L587 Update gibct-data-service Rails to version 5.0.7.2 #19605  To retrieve objects from the database, Active Record provides several finder methods. Each finder method allows you to pass arguments into it to perform certain queries on your database without writing raw SQL. As per the changelog, it says Add 'ActiveRecord::Relation#annotate' for adding SQL comments to its queries.

When you are using new definetly u need to save that variable For example

organization=Organization.new
organization.name = "organization 1"
organization.description= "Something description"
organization.save #save the array obj
Organization.count #1

else use create method to create the record Suddenly both are same

Organization.create(name:"org2",description: "something")
Organization.count #2

Rails 5.0.1 issue-- undefined method `push' for #<User , The activerecord array should include the new record into its collection undefined method `push' for #User::ActiveRecord_Relation:0x0000000aba23b8 Yes, a Relation object doesn't respond to those methods because  ActiveRecord::Relation#or returns a new relation, which is the result of filtering the current relation according to the conditions in the arguments, joining WHERE clauses with OR operand, contraty to the default behaviour that uses AND.

This works for me:

a=[]
a << @report.organizations
a.count # => 1
a << Organization.new
a.count # => 2

Active Record's << or push or concat method, posted in Ruby and Rails on August 4, 2011 by Gerry gotchas you have to keep in mind when you are using ActiveRecord's << method of a has_may association. does it saves the object before adding it to the collection or waits for the owner to get saved and then saves the Amit Patel September 6, 2012 at 5:59 am. Steps to reproduce 1.) Create a basic model 2.) Assign a collection subset of that model to a variable: @hours = Hour.all 3.) Try to push another instance of the model into that collection @hours << Hour.new() Expected behavior A new bla

Active Record Associations, The belongs_to Association; The has_one Association; The has_many Association; The class CreateAppointments < ActiveRecord::Migration[ 5 . 0 ] To override this behavior (and force a database read), call #reload_association on the parent object. This method is aliased as collection.concat and collection.​push . none (rails 4 only) Likewise, sometimes you want an ActiveRecord::Relation that contains no objects. Returning an empty array is usually not a great idea if the consumer of your API is expecting a relation object. Instead, you can use none.

How to push an object into ActiveRecord::Relation in rails 5, How to push an object into ActiveRecord::Relation in rails 5. I am stuck in weird situtaion, I am working on upgrading an existing rails 3  To continue using the scoped relation, pass it into the block directly. To instead access the full set of models, as Rails 6.1 will, use `#{name}.default_scoped`. MSG end if self == scope. klass scope. clone else relation. merge! (scope) end else default_scoped end end

The 10 Most Common Mistakes That Rails Developers Make, There are many Ruby on Rails best practices that can help you avoid these While this still pushes the limits of the single responsibility principle, it's sort of the Common Mistake #5: Using too many gems This is done by calling the includes (or preload ) method on the Arel ( ActiveRecord::Relation ) object being built. Active Record BasicsThis guide is an introduction to Active Record.After reading this guide, you will know: What Object Relational Mapping and Active Record are and how they are used in Rails. How Active Record fits into the Model-View-Controller paradigm. How to use Active Record models to manipulate data stored in a relational database. Active Record schema naming conventions. The concepts

Comments
  • will you save this record later ?
  • if no , directroly use @report.organizations.count + 1
  • Do you get same error for rails-4 also?
  • Why it shows like #<Organization::ActiveRecord_Relation instead of Organization::ActiveRecord_Associations_CollectionProxy
  • Not sure why may be in rails 5 it is like that only, also i didn't checked in rails 4 because I am upgrading from rails 3 to rails 5 directly :-)
  • for some reason, I don't want to create new organization instead I need to push new org in activerecord relation without creating
  • If the count was changed in Rails 4, you were effectively creating a new org
  • Since this is simply a class and not extended by activerecord base class, organziations is an attribute_reader in report class whose value is calculated by finding org from database.