Skip to content

Instantly share code, notes, and snippets.

@jdudek jdudek/tags.rb
Created Jul 6, 2011

Embed
What would you like to do?
create_table "tags", :force => true do |t|
t.string "name", :default => ""
end
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
@jdudek

This comment has been minimized.

Copy link
Owner Author

commented Jul 6, 2011

Tak by to wygladalo bez ARela:

def self.tags
 Tag.where(:taggable_type => "Post", :taggable_id => self.select(:id))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.