Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord::Relation, any way to remove existing limit and offset?

Assuming I have a User model

paged_users = User.scoped.limit(2).offset(3)

Is there a way to make paged_user have User.scoped by removing limit and offset? IE:

all_user = paged_users.remove_limit.remove_offset
like image 656
allenhwkim Avatar asked Mar 14 '12 20:03

allenhwkim


People also ask

What is ActiveRecord :: Base in Rails?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What is ActiveRecord in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

How do you delete a record in rails?

Rails delete operation using destroy method By using destroy, you can delete the record from rails as well as its other existing dependencies. So in the context of our rails application, if we delete a book record using the destroy function, the authors associated with the book will also be deleted.

What is offset in Ruby?

Ruby | DateTime offset() function DateTime#offset() is a DateTime class method which returns the DateTime object offset. Return: the DateTime object offset.


1 Answers

I'm thinking you have a scope like this:

users = User.where("something").limit(20).order("name ASC")

With this in mind...

To remove the limit pass nil to limit:

users.limit(nil)

Then to remove the ordering, use reorder, also passing it nil:

users.limit(nil).reorder(nil)

That will remove both the limit and the order from your scope, preserving all other things. If you were to use unscoped, it would remove all scoping.

like image 58
Ryan Bigg Avatar answered Oct 08 '22 18:10

Ryan Bigg