#85 ✓invalid
Stet

failures linked to association extensions

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

(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!

Comments and changes to this ticket

  • Chris Wanstrath

    Chris Wanstrath August 17th, 2007 @ 10:18 PM

    • Assigned user changed from “Chris Wanstrath” to “Mislav”
    • State changed from “new” to “open”
  • Mislav

    Mislav October 7th, 2007 @ 06:23 PM

    • 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.

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.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Everyone's favorite Ruby library for pagination of practically anything!

People watching this ticket

Pages