0

I have a Sinatra app using the Sequel ORM in which I'm trying to list only Categories that have one or more Posts.

So, if I have two categories in the database; "Apples" and "Oranges", and one Post assigned to "Apples", then when I list the current categories I only want the "Apples" category to be provided.

After much hair-pulling I finally managed to get it working with the following;

class Post < Sequel::Model
  many_to_one :category
end

class Category < Sequel::Model
  one_to_many :posts

   dataset_module do 
     def with_posts
       where(id: Post.select(:category_id))
     end
   end
end

@categories = Category.with_posts

If there's a better way of doing this in Sequel please do let me know.

1
  • What you have is probably the best way to do it. Feb 26, 2015 at 22:24

1 Answer 1

0

Try Jeremy's counter cache https://github.com/jeremyevans/sequel_postgresql_triggers

# In your migration:
pgt_counter_cache :categories, :id, :posts_count, :posts, :category_id

# And in your code:
categories = Category.exclude(posts_count: 0).all

The documentation isn't very good so here are the arguments: https://github.com/jeremyevans/sequel_postgresql_triggers/blob/master/lib/sequel_postgresql_triggers.rb#L27

1
  • I ofter use counter cache when in Rails so I like this, and as I use postgres exclusively these days, I don't mind that dependency. Will definitely take a look at this. Thanks!
    – mrcook
    Feb 27, 2015 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.