Combining acts_as_taggable, will_paginate and scope_out


#1

I want to display published posts that are tagged using will_paginate.
Options include:

  1. Add :conditions => { :published => true } to the find options. Does
    not
    work because will_paginate will then return too many records.
  2. Use scope_out in class Post like this:

scope_out :published, :conditions => { :published => true }

Controller:

def tagged_with
@tag = params[:id]

# Thanks go out to http://blog.wolfman.com/posts/33
options = Post.find_options_for_find_tagged_with(@tag).merge(:page 

=>
params[:page], :per_page => property(:items_per_page))
#@posts = Post.paginate(options)
@posts = Post.paginate_published(options)

redirect_to :action => :tag_not_found if @Posts.empty?

# Create a new comment in memory
@comment = Comment.new( :Post => @Post)

end

But this yields the error:

test_show_nonpublished_posts(FrontendControllerTest):
ActiveRecord::RecordNotFound: Couldn’t find Post without an ID
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1572:in
find_from_ids' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:616:infind’
(eval):2:in find_published' (eval):4:inwith_published’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2148:in
with_scope' (eval):3:inwith_published’
(eval):2:in `find_published’

Any ideas why?


#2

Found! :smiley:

Model:

XXL name to prevent override meta-methods such as paginate_published

def self.paginate_all_published_posts(options)
self.with_published do
return self.paginate(options)
end
end

Controller:

@posts = Post.paginate_all_published_posts(options)

On Sun, Apr 26, 2009 at 4:52 PM, Commander J. <