Last active
December 15, 2015 16:49
-
-
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?..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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