Codium
Codium

Reputation: 3240

No MethodError when trying get active record id

In model:

  def self.get_by_slug(slug)
    self.where("slug = ?", slug)
  end

In controller:

  #route: match '/category/:slug', :to => 'category#index', :as => "category_jobs"
  @category = Category.get_by_slug(params[:slug])
  @jobs = Job.where("category_id = ? AND expires_at > ?", @category.id, DateTime.now)
           .paginate(:page => params[:page], :per_page =>    Jobeet::Application::MAX_JOBS_ON_CATEGORY_PAGE)
           .order("expires_at desc")

When I trying get category.id in controller I am getting error:

undefined method `id' for #

Could somebody give me any advice?

Upvotes: 0

Views: 76

Answers (1)

forker
forker

Reputation: 2679

If you expect a single record you should do:

@category = Category.get_by_slug(params[:slug]).first

because .where(something) doesn't return a single record.

Upvotes: 3

Related Questions