ruby on rails - Pluck and ids give array of non unique elements -
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" "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(*) "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
you should able work around with
learn::course.pluck('distinct learn_courses.id')
the problem left outer join
learn_course_translations
, must have multiple rows per learn::course
, resulting in same learn_courses.id
appearing several times. pluck
doesn't care distinctness, passes them back.
Comments
Post a Comment