Converting an array of objects to ActiveRecord::Relation
How can I convert an Array of objects to an ActiveRecord::Relation? Preferably without doing a where each time.
You cannot convert an Array to an ActiveRecord::Relation since a Relation is just a builder for a SQL query and its methods do not operate on actual data.
However, if what you want is a relation then:
for ActiveRecord 3.x, don’t call
all
and instead callscoped
, which will give back a Relation which represents the same records thatall
would give you in an Array.for ActiveRecord 4.x, simply call
all
, which returns a Relation.
When running a
def self.subjects
type method on an ActiveRecord::Relation, how do I access that ActiveRecord::Relation object itself?
When the method is called on a Relation object, self
is the relation (as opposed to the model class it’s defined in).
How can i convert an array to an rails ActiveRecord::Relation without changing array order?
This seems to do what you want (res1 ... res5
should be Restaurant
objects)
ids = [res1,res5,res2,res4,res3].map(&:id)
@places = Restaurant.where(id: ids).order("FIELD(id, #{ids.join(',')})").all
More info, Maintaining order in MySQL "IN" query
Rails 5 - How to convert from array to activerecord relation?
If you just want the associations for the current user and you've set up the 'has_many' correctly you can do...
current_user.associations.order(created_at: :desc)
This will give you all the association records, and (of course) the current_user information like id
and name
are available directly from the current user.
This will be an ActiveRecord::Relation and you can iterate over it so, yes, it'll behave like an array. If you want all the list, you want an array or array-like structure.
Rails 5 rspec - convert object to ActiveRecord Relation
The simple way to do it is to use create_list
and return an ActiveRecord
relation, but if I were you I'd rethink it to use stubs
.
let(:array) do
list = create_list(:class_example, 4)
ClassExample.where(id: list.map(&:id))
end
Ruby: Convert an array or hash to an activerecord relation
What you want to do start from the inverse end (Category) and use an aggregate in the ORDER clause.
Category.joins(:articles)
.order('SUM(articles.impressions_count) DESC')
.group(:id)
.limit(5)
irb(main):005:0> Category.joins(:articles).order("SUM(articles.impressions_count) DESC").group('categories.id').limit(5)
Category Load (1.5ms) SELECT "categories".* FROM "categories" INNER JOIN "articles" ON "articles"."category_id" = "categories"."id" GROUP BY categories.id ORDER BY SUM(articles.impressions_count) DESC LIMIT $1 [["LIMIT", 5]]
=> #<ActiveRecord::Relation [#<Category id: 4, name: "C#", created_at: "2017-11-15 15:06:32", updated_at: "2017-11-15 15:06:32">, #<Category id: 3, name: "JavaScript", created_at: "2017-11-15 15:06:32", updated_at: "2017-11-15 15:06:32">, #<Category id: 1, name: "Simula", created_at: "2017-11-15 15:03:37", updated_at: "2017-11-15 15:03:37">, #<Category id: 2, name: "Haskell", created_at: "2017-11-15 15:06:32", updated_at: "2017-11-15 15:06:32">]>
And you should create a class method - not an instance method as this is basically just a scope and does not make sense to call on an instance.
class Category < ApplicationRecord
has_many :articles
def self.order_by_article_impressions
self.joins(:articles)
.order('SUM(articles.impressions_count)')
.group(:id)
end
def self.top_5_visited
order_by_article_impressions.limit(5)
end
# Or use `scope` which is just syntactic sugar
scope(:top_5_visited) -> { order_by_article_impressions.limit(5) }
end
How to Convert ActiveRecord_Relation to an normal array in rails
Relation has to_a
method, which does exactly that. It is used internally for methods like any?
Related Topics
Rvm 'Not Found' After Successful Usage and a Few Days Later
Limit Space and Memory Used by Imagemagick
Confirmation About Pgrep Returning Itself
Can't Install Ruby Rvm on Ubuntu 16.04 Due to Gpg Bug
Rails Assets:Precompile Yarn Executable Was Not Detected in the System
Ruby: "Gem Install Bundler" Not Installing Bundler
How to Compile and Statically Link Ruby Libs for Docker
Putting French (Accented) Characters in Ruby File
Counter_Cache With Has_Many :Through
Really Cheap Command-Line Option Parsing in Ruby
Rails Console: Reload! Not Reflecting Changes in Model Files? What Could Be Possible Reason
Passing a Method as a Parameter in Ruby
Ruby: How to Copy a Variable Without Pointing to the Same Object