why .limit not working with find_by
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.
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.