5

In console:

Course.ids.count 
=> 1766
Course.pluck(:id).count
=> 1766
Course.ids.uniq.count
=> 1529
Course.count
=> 1529

It's normal?

small comment - model Course uses ancestry (gem).

UPD1:

Generated sql:

Learn::Course.ids.count
(5.4ms)  SELECT "learn_courses"."id" FROM "learn_courses" LEFT OUTER JOIN "learn_course_translations" ON "learn_course_translations"."learn_course_id" = "learn_courses"."id"
=> 1766
Learn::Course.count
(1.5ms)  SELECT COUNT(*) FROM "learn_courses"
=> 1529

hmm...

UPD2:

Schema Information

#
# Table name: learn_courses
#
#  id          :integer          not null, primary key
#  name        :string(255)
#  position    :integer
#  created_at  :datetime
#  updated_at  :datetime
#  ancestry    :string(255)
#  course_type :string(255)
#  article     :string(255)
#  item_style  :integer
#  hidden      :boolean
#  score       :integer          default(0)
#  next_id     :integer
#  first       :boolean
Gena Shumilkin
  • 713
  • 4
  • 16

2 Answers2

1

You should be able to work around this with

Learn::Course.pluck('distinct learn_courses.id')

The problem is that LEFT OUTER JOIN with learn_course_translations, which must have multiple rows per Learn::Course, resulting in the same learn_courses.id appearing several times. pluck doesn't care about distinctness, so it just passes them all back.

Kristján
  • 18,165
  • 5
  • 50
  • 62
  • 1
    I see. Looks like you are right. One more point: `Learn::Course.pluck('distinct id')` returns `PG::AmbiguousColumn: ERROR: column reference "id" is ambiguous`. But it does not matter, I realized what the problem is. Thanks. – Gena Shumilkin Jun 25 '15 at 16:04
0

Maybe ancestry adds default_scope to your model. Try to check it with

Learn::Course.unscoped.ids.count
hedgesky
  • 3,271
  • 1
  • 21
  • 36