So i'm getting these three queries on my console:
D, [2014-05-30T09:41:15.504655 #4629] DEBUG -- : (0.2ms) SELECT COUNT(*) FROM "posts" WHERE ("posts"."published_on" IS NOT NULL)
D, [2014-05-30T09:41:15.506857 #4629] DEBUG -- : Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE ("posts"."published_on" IS NOT NULL) ORDER BY published_on DESC LIMIT 1 OFFSET 0
D, [2014-05-30T09:41:15.513479 #4629] DEBUG -- : (0.3ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "posts" WHERE ("posts"."published_on" IS NOT NULL) LIMIT 1 OFFSET 0) subquery_for_count
I did google this and found this 'N+1 queries' and a whole lot but not real answers why; so i don't know if i should fix this or not, or how (im new to Ruby/sinatra)
On my code all i do is:
count = Post.where.not({published_on: nil}).count
....
@posts = Post.where.not({published_on: nil}).order("published_on DESC").limit(per_page).offset(offset)
In the middle i do some calculations with count to ge the offset, and after that a @post.length to know if i get any results.
My models are pretty simple, Post(w/has_many tags), Tag, and PostTags to associate both.
Any tip to change this, if i have to?
Thanks