Ronald Fischer
2014-Aug-16 10:03 UTC
Issues with catenating the results of two queries + will_paginate
(Rails 4, Ruby 2) I have two functions which query the database. Both yield a set of model objects. These two sets should be catenated and presented to the user using the will_paginate Gem. I think I understand now how to do it, but a few issues with this are still puzzling me. Here are the query functions: def the_other_dicts_of_same_user(d) User.find_by_id(d.user_id).dicts.where("id != #{d.id}") end def public_dicts_of_other_users(d) Dict.where("user_id != #{d.user_id} and world_readable") end Both yield a series of Dict objects. The result of the first query is of type Dict::ActiveRecord_AssociationRelation and the result of the second query is of type Dict::ActiveRecord_Relation I guess I get different data types here, because I start with User in the first case and with Dict in the second case. As a side note: I *could* have written the first query alternatively as Dict.where("user_id = #{d.user_id} and id != #{d.user_id}") Would this be better? Anyway, I catenate the results, @dicts=the_other_dicts_of_same_user(d)+public_dicts_of_other_users(d) which yields again a Dict::ActiveRecord_Relation . Now I apply pagination: @dicts=@dicts.paginate(....) This yields the error message, that paginate is not defined for objects of type Array. I was able to get around this error, by doing a require 'will_paginate/array' as a first line in my controller, but I wonder: Why do I get this error? @dicts is NOT an array (I logged @dicts.class.to_s to be sure of this), and I would have expected to work it in a ActiveRecord_Relation. Could someone please kindly explain this observed behaviour to me? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d85f9c9847591cb08260ea24d7f84554%40ruby-forum.com. For more options, visit https://groups.google.com/d/optout.