undefined method `default_order' for []:Array

30 views
Skip to first unread message

Jim Harvey

unread,
Oct 24, 2011, 11:05:43 PM10/24/11
to hobo...@googlegroups.com
My application uses a License model which has_many Libraries, which, in turn, has_many Books. A user has a License associated with their account that should allow them to see the Books included in their licensed Library. I have this in the Books controller:

def index
    filtered_books = Book.apply_scopes( 
      ...
    )    
    hobo_index filtered_books.published.licensed_to(current_user)
end

but it is throwing this error when I try to load the index: undefined method `default_order' for []:Array

in Book.rb, we have: 

def self.licensed_to(user)
  user.accessible_libraries.*.books
end

This returns an Array, for which default_order is not defined. Here is the accessible_libraries method in User.rb:

def accessible_libraries
  self.user_licenses.active.*.libraries
end

So my problem is: How can I either avoid the default_order call in hobo_index or refactor this so that I can pass something to hobo_index that responds to default_order instead of a plain Array? Any advice would be much appreciated. Thanks!

Jim

Tomoaki Hayasaka

unread,
Oct 25, 2011, 12:12:52 AM10/25/11
to hobo...@googlegroups.com, jimrh...@gmail.com
Two things:

1. Many rapid tags depend on Hobo metadata (e.g. #member_class). I
couldn't figure out whether it's applicable or not on your case, it
might be lost in your filter chain.

2. find_or_paginate() requires listing order specified, either by the
:order option or by model.default_order. I think it's a bug in Hobo
because really a simple case `hobo_index ::User.all` will fail.


Here's a workaround:

def index
filtered_books = Book.apply_scopes(
...
)

filtered_published_licensed_books = filtered_books.published.licensed_to(current_user)
def filtered_published_licensed_books.member_class # for 1., not required if member_class is properly propagated
Book
end
hobo_index filtered_books.published.licensed_to(current_user), :order => Book.default_order # for 2.
end

-----
Tomoaki Hayasaka <haya...@pfsl.mech.tohoku.ac.jp>

kevinpfromnm

unread,
Oct 25, 2011, 11:56:37 AM10/25/11
to hobo...@googlegroups.com
You might get around this by making your licensed_to a scope instead so you can pass it straight to the hobo_index.  Also solves the pagination problem.

Jim Harvey

unread,
Oct 25, 2011, 12:58:13 PM10/25/11
to hobo...@googlegroups.com
Thanks guys. Actually, I was originally trying to make licensed_to a scope but in order to chain it in the BookController, I'd need to pass in the current_user and I'm not sure that it is possible to pass a variable to a scope. If I try to use Hobo's acting_user variable inside any model but outside of the 4 permissions methods, it complains that acting_user is not defined. At this point though, scopes seem to be my best bet without explicitly defining the member_class, which I'm not sure I want to do. If you have any ideas about how to make scopes work in this case, please let me know. 

Also, one of the reasons I'm dealing with this is because of the pagination problem. Again, scopes do seem to be the answer, just not sure of the implementation. Thanks!


On Tue, Oct 25, 2011 at 8:56 AM, kevinpfromnm <kevinp...@gmail.com> wrote:
You might get around this by making your licensed_to a scope instead so you can pass it straight to the hobo_index.  Also solves the pagination problem.

--
You received this message because you are subscribed to the Google Groups "Hobo Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/hobousers/-/sbdWfVh_9HcJ.

To post to this group, send email to hobo...@googlegroups.com.
To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.

Jim Harvey

unread,
Oct 25, 2011, 1:20:48 PM10/25/11
to hobo...@googlegroups.com
Hmm, so it looks like I can pass variable to a scope after all, which makes the scope definition look something like this:

scope :licensed_to, lambda { |user| 
  ...
}

Will post again when I figure out what goes in place of the ... :D

Jim Harvey

unread,
Nov 4, 2011, 2:17:27 AM11/4/11
to hobo...@googlegroups.com
Finally figured this out! Needed to use Hobo's association scopes: http://cookbook-1.3.hobocentral.net/manual/scopes#association_scopes

in Book.rb

scope :licensed_to, lambda { |user|
  any_of_libraries(Library.licensed_to(user))
}

and thus in Library.rb

scope :licensed_to, lambda { |user|
  any_of_user_licenses(UserLicense.user_is(user))
}

And then the same Finder call I have in my original post in BooksController works, with the correct pagination. Awesome.
Reply all
Reply to author
Forward
0 new messages