Skip to content

Instantly share code, notes, and snippets.

@Greenie0506
Created March 12, 2012 01:39
Show Gist options
  • Save Greenie0506/2019193 to your computer and use it in GitHub Desktop.
Save Greenie0506/2019193 to your computer and use it in GitHub Desktop.
class Post < ActiveRecord::Base
attr_accessor :tag_names
has_many :post_tags, :dependent => :destroy
has_many :tags, :through => :post_tags
after_save :assign_tags
def assign_tags
if @tag_names
self.tags = @tag_names.split(/\s+/).map do |name|
Tag.find_or_create_by_name(name)
end
end
end
def self.all_matching_tags(tag)
tags = tag.split(",")
tags = tags.map{ |tag| Tag.find_by_name(tag) }
post = Post.all.select{ |p| (p.tags & tags).length == tags.length }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment