Skip to content

Instantly share code, notes, and snippets.

@ZackMattor
Last active December 15, 2015 16:49
Show Gist options
  • Select an option

  • Save ZackMattor/5292165 to your computer and use it in GitHub Desktop.

Select an option

Save ZackMattor/5292165 to your computer and use it in GitHub Desktop.
Trying to get more fields in my query, other than what the model represents?..
Console output:
7: def popular_tags
8: #Tag.find(:all)
9: tags = select('max(topics.tag_id) as "id", count(*) as "number_tagged", max(tags.title) as "title"').joins(:topics).group('tag_id').find(:all)
=> 10: binding.pry
11: end
[1] pry(Tag)> tags
=> [#<Tag id: 15, title: "Java">]
<MODEL CODE>
class Tag < ActiveRecord::Base
attr_accessible :title, :forum_thread_ids, :topic_ids
has_many :topics
has_many :forum_threads, through: :topics
class << self
def popular_tags
#Tag.find(:all)
tags = select('max(topics.tag_id) as "id", count(*) as "number_tagged", max(tags.title) as "title"').joins(:topics).group('tag_id').find(:all)
binding.pry
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment