Skip to content

Instantly share code, notes, and snippets.

@jdudek
Created July 6, 2011 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdudek/1068324 to your computer and use it in GitHub Desktop.
Save jdudek/1068324 to your computer and use it in GitHub Desktop.
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.string "taggable_type", :default => ""
t.integer "taggable_id"
end
class Post
has_many :taggins, :polymorphic => true
def tags
tag_used_in_scope = Tagging.arel_table[:taggable_id].in(self.select(:id).arel)
taggable_has_correct_type = Tagging.arel_table[:taggable_type].eq(self.klass.to_s)
Tag.where(tag_used_in_scope.and(taggable_has_correct_type))
end
end
blog.posts.tags => # wszystkie tagi, ktore wystepuja w postach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment