Mik378
Mik378

Reputation: 22191

Rails - How does 'will_paginate' plugin work?

Will_paginate plugin allows whatever ActiveRecord to get paginated.

Indeed, a paginate method is available within this ActiveRecord. For example, if I want to paginate Order, I do:

Order.paginate page:params[:page], order: 'created_at desc'

How paginate method is created? Is this the Will_paginate plugin that does some kind of: "taking ActiveRecord definition and injecting a paginate method into?

Furthermore, to keep this example, if the above assumption is correct:

How to avoid conflict with the plugin if for whatever reason, Order has defined a proper paginate with a totally different purpose?

Upvotes: 0

Views: 756

Answers (1)

drummondj
drummondj

Reputation: 1483

will_paginate uses the extend method to add modules to the ActiveRecord::Base class. See https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/active_record.rb#L202

If Order also has a method called paginate then it will override the ActiveRecord::Base#paginate method.

Upvotes: 2

Related Questions