Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

why .limit not working with find_by

Rails • Asked by TG

am trying to retrieve date with @category = Category.find_by(slug: params[:slug]).limit(5) but i get undefine method .limit


find_by only returns the first object that matches your query. You're calling limit on an instance of your category model which causes this error. Try using Category.where instead, .where returns an array instead of an object.


I need to get that +1 feature added so I can +1 Andy's comment. :)


Thanks Chris! Thanks for all you do.


You're welcome! Thanks for being here! :D


Category.where would fail so am able to fix it with
also using find_by since am looking for only one record

@categories = Category.find_by(slug: params[:slug])
@category = @categories.products.paginate(:page => params[:page], :per_page => 20).order('created_at DESC')


For clarity, you might want to swap your variable names, because @categories is actual a single Category, and @category is an array of Categories.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 31,353+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.