ruby on rails - Is it possible to filter by conditions before paginating? -
I am using Ruby on Rail 2.3.8 and I_paginate the plugin.
I have just seen that if I write something like this:
Declaration. Pages: Page = & gt; Param [: page],: per page = & gt; 10,: conditions = & gt; announcements = declaration. All: Conditions = & gt; Some_condition @ann = announceAngenet.net: Page = & gt; Param [: page],: per page = & gt; 10
These conditions will not be recognized.
Edit:
I have developed a search functionality and, due to the serial functionality I had to implement, I had a model inside Search counseling has to call it every time I need to search or sort from the area, from the controller.
Therefore, the methods of your model will look like this:
DEF self.search_by_relevance (word) status = get_search_conditions (word) Announcement.published.descend_by_featured.order_by_rate: status = & Gt; Position end
Where "published" and "order_by_rate" are given the name Scope and "searchlogic" gem from "descend_by_feature".
Def belongs to self.get_search_conditions (words) until words.empty? Or words.nil? Terms = '' words.each do | Words | If conditions.nil? Or conditions Title like 'conditions' =' ('%' + word + '%' or '%' + word + '%') 'other conditions + =' and (like title "% '+ word +'%" or details like "% '+ Word +'%") 'end-end position end date
The action of my controller looks like this:
def search @ Announcements = announcement .search_by_relevance (params [: txtSearch] .to_s.split). Paginated: Page = & gt; Param [: page],: per page = & gt; Until 10 Params [: txtSearch] .nil? Or consultation [: txtSearch] Liberation? Edit 2:
Thanks for the post, test my code a bit and I found out that If I type ".all" on this line immediately after "order_by_rate" then announcement. Posted on. Dscend_by_featured.order_by_rate: conditions = & gt; This correct query will return to the search_by_relevance method, but will_paginate plugin will give me the following error (just if I add "all of all"): NoMethodError in AnnouncementsController # Undefined method for search `to_i '{: page = & gt; Zero ,: per_page = & gt; 10}: hash D: /Proyectos/Cursometro/www/vendor/plugins/will_paginate/lib/will_paginate/collection.rb: 15: In 'Start' D: / Proyectos / Cursometro / www / vendor / plugins / will_paginate / lib / Will_paginate / core_ext.rb: 37: `new in 'D: / Proyectos / Cursometro / www / vendor / plugins / will_paginate / liberalization / will_paginate /core_ext.rb37:in` pagination' D: / Proyectos / Cursometro /www/app/controllers/announcements_controller.rb: 276: In 'search'
First of all, I do not understand that ".all" Where to add, and second, I do not think why I_paginate is not working while adding "I" (I also tried to add the following code but did not work: : page = & gt; parameter [: page] || 1
).
In addition, if I include the ".all" syntax in the query, it will return:
SELECT * FROM announcements
Order by WHERE ("% anuncio%" such as title or description like "% Anuncio%")) and declarations (stats = 'published') announcements. Featured DESC
If I do not, then it will return:
SELECT * FROM announcements
WHERE (announcements.state = 'Published') Order by announcements. Featured DESC
Do you see that there is no situation involved in the last one? This is causing the problem.
I do not know that this will work for you, but you can change the paginate
Use Find
, I mean, something like this:
@ announcements = announcement.paginate_all_by_id params [: id],: page = & gt; Param [: page],: per page = & gt; 10
Edit
@ Announcements
is an array, is not it?
Well, post me and find one that can help you.
Comments
Post a Comment