Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

failures linked to association extensions

#85

(Apologies if I've duped this -- I'm having trouble creating a ticket in Lighthouse)

I'm using an association extension in one of my models, to help with a per-user hand-rolled tagging setup:

class Tag < ActiveRecord::Base
  has_many :taggings
  has_many :prints, :through=>:taggings, :source=>:print, :uniq=>true do
    def for_user(user)
      find :all, :conditions=>["taggings.user_id = ?", user.id]
    end
  end

When I try to call something like:

@tag = Tag.find_by_title("sometag")
@prints = @tag.prints.for_user(current_user).paginate(:page=>params[:page])

It fails with:

undefined method `to_i' for {=>nil}:Hash

and the trace:

vendor/plugins/will_paginate/lib/will_paginate/collection.rb:16:in `initialize'

So, poking about, I edited /vendor/plugins/will_paginate/lib/will_paginate/collection.rb:16 to say:

 def initialize(page, per_page, total)
      if page.is_a?(Hash)
        page = page[:page]
      end
      @current_page  = page.to_i
      #continues as normal...

Which partially fixed the problem -- for results sets larger than 15. If the results are larger than 15, the pagination does appear -- and works if you click directly on the "2" or "3" links, although the "1" link is not highlighted, and the "next" link points to the current page.

If the results set is under 15, you get this error:

Showing app/views/shared/_printlist.rhtml where line #18 raised:

undefined method `page_count' for []:Array

(line 18 is the will_paginate line)

The relevant trace line points at:

vendor/plugins/will_paginate/lib/will_paginate/view_helpers.rb:41:in `will_paginate'

So, I'm turning it over to you guys!

Reported by Stet · August 17th, 2007 @ 10:18 PM

State: invalid
Milestone: none
Assigned to: Mislav Mislav

Activity

  1. Chris Wanstrath
    Chris Wanstrath
    • Assigned user changed from Chris Wanstrath to Mislav
    • State changed from new to open

    August 17th, 2007 @ 10:18 PM

  2. Mislav
    Mislav
    • State changed from open to invalid

    Doh! You're using it all wrong. Do something like this

      has_many :prints, :through=> :taggings, :source=> :print, :uniq=>true do
        def find_for_user(user, params = {})
          with_scope :find => { :conditions => ["taggings.user_id = ?", user.id] } do
            find :all, params
          end
        end
      end
    

    Then you can:

    @tag.prints.paginate_for_user(current_user)
    

    Note that you'll probably have to do the count yourself.

    October 7th, 2007 @ 06:23 PM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.