I have a simple app that allows users to post text and images. I'm also using the thumbs_up gem (github repo) so users can vote on their favorites.
Currently, I'm trying to create one feed that has both text and image items. I've been able to combine both into one aggregated feed, order it, and the views work. However, when I use the "rank_tally" method on @feed in the controller, I get an "undefined method `rank_tally' for #Array:0开发者_Go百科x1052befe8" error.
rank_tally works if I apply it to one model only (e.g. if i change @aggregated to posts.rank_tally), but there seems to be a problem with the @aggregated array I'm creating by combing both.
Any ideas on how I can fix this? Thank you very much for the help!
def feed
@user = User.find_by_cached_slug(params[:id])
posts = Post.where("poster = ? and (postername is not null or name != ?)", @user.id, 'Guest')
images = Image.where("poster = ? and (postername is not null or name != ?)", @user.id, 'Guest')
@aggregated = (posts + images).rank_tally(
{:at_least => 1,
:at_most => 10000,
:start_at => 1.weeks.ago,
:limit => 10,
})
@feed = @aggregated.paginate(:page => params[:page])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
It's because the result of (posts + images) is an array, not relation, so it can't work. You should try this:
posts.merge(images).rank_tally(...)
精彩评论