(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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
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.

Search Discussions

  • Walter Lee Davis at Aug 16, 2014 at 1:31 pm

    On Aug 16, 2014, at 6:03 AM, Ronald Fischer wrote:

    (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?
    I suspect that it is because a Relation is not an Array, it's more of a scope. It can be iterated over, because it extends Enumerable, but that does not make it an array. But adding the two together must (I am guessing here) cause them to both be evaluated as arrays before the addition can succeed. When you do to_a on a Relation (either kind) it loses its magic lazy-loading behavior, and the elements are found and placed into the array. The whole point of a Relation is that the found records inside it are not loaded into memory until they are actually asked for more formally. This makes it much faster (and allows for further chaining) in a scope.

    Walter
    --
    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@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    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.
    --
    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@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/AD6E900D-09C4-45A9-969F-414AE9AE1582%40wdstudio.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ronald Fischer at Aug 16, 2014 at 1:42 pm

    Walter Davis wrote in post #1155317:
    But adding the two together must (I am
    guessing here) cause them to both be evaluated as arrays before the
    addition can succeed.
    I don't think this is the case. As I said, I also logged the object
    AFTER adding them together, and it still is a
    Dict::ActiveRecord_Relation. The conversion into an Array must come
    after that, and this means it must happen inside paginate().

    Ronald

    --
    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@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/88b28ae3f6e5c76efda84466ad54dd37%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Walter Lee Davis at Aug 16, 2014 at 1:46 pm

    On Aug 16, 2014, at 9:42 AM, Ronald Fischer wrote:

    Walter Davis wrote in post #1155317:
    But adding the two together must (I am
    guessing here) cause them to both be evaluated as arrays before the
    addition can succeed.
    I don't think this is the case. As I said, I also logged the object
    AFTER adding them together, and it still is a
    Dict::ActiveRecord_Relation. The conversion into an Array must come
    after that, and this means it must happen inside paginate().
    Is it possible that you are getting this:

    [ ActiveRecord_Relation, ActiveRecord_AssociationRelation ]

    (an array of the outermost objects) rather than this:

    [ #Record 1, #Record 2, ... ]

    when it gets to pagination?

    Walter
    Ronald

    --
    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@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/88b28ae3f6e5c76efda84466ad54dd37%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2CE83A07-261D-4351-8B09-436C4EAB28B4%40wdstudio.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ronald Fischer at Aug 16, 2014 at 1:56 pm

    Walter Davis wrote in post #1155322:
    On Aug 16, 2014, at 9:42 AM, Ronald Fischer wrote:

    Walter Davis wrote in post #1155317:
    But adding the two together must (I am
    guessing here) cause them to both be evaluated as arrays before the
    addition can succeed.
    I don't think this is the case. As I said, I also logged the object
    AFTER adding them together, and it still is a
    Dict::ActiveRecord_Relation. The conversion into an Array must come
    after that, and this means it must happen inside paginate().
    Is it possible that you are getting this:

    [ ActiveRecord_Relation, ActiveRecord_AssociationRelation ]
    No, because I printed .class.to_s to the object after catenation, and if
    it would be an array of whatever content, this would have printed
    "Array", but it printed "Dict::ActiveRecord_Relation".

    That's why I conclude that the conversion to an Array must happen within
    the paginate method itself.

    Ronald

    --
    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@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e46be0444635568adf2b85379baa71bc%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Aug 16, 2014 at 1:38 pm

    On 16 August 2014 11:03, Ronald Fischer wrote:
    (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 it be possible to combine the two queries into one - Dict.where(
    ... or ... )? Then you would not need to concatenate them.

    Colin

    --
    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@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsirLws6YiFyhVU2ggX5JjD-AtjBQXNNiX5AEmkSdfXMQ%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ronald Fischer at Aug 16, 2014 at 1:45 pm

    Colin Law wrote in post #1155319:
    On 16 August 2014 11:03, Ronald Fischer wrote:
    Would it be possible to combine the two queries into one - Dict.where(
    ... or ... )? Then you would not need to concatenate them.
    Technically, yes, and it likely would be a performance improvement too.
    From a designer's viewpoint, I don't like this solution so much, because
    the individual parts are used in different contexts too, and it means I
    would have code duplication here, but maybe I will give it a try.

    Ronald

    --
    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@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f36a8039a96f3275ff1926a9fee19a63%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Scott Ribe at Aug 16, 2014 at 1:58 pm

    On Aug 16, 2014, at 7:45 AM, Ronald Fischer wrote:

    and it means I
    would have code duplication here, but maybe I will give it a try.
    Actually, you can build the relations separately, and then combine them with .or, before accessing rows of the result. So you probably do not have get code duplication from that approach.

    --
    Scott Ribe
    scott_ribe@elevated-dev.com
    http://www.elevated-dev.com/
    (303) 722-0567 voice




    --
    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@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/A1398DC1-A1E5-40C5-9A3D-90263E05B4B2%40elevated-dev.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedAug 16, '14 at 10:03a
activeAug 16, '14 at 1:58p
posts8
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase