Skip to content

Instantly share code, notes, and snippets.

@carlesso
Created June 22, 2011 14:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlesso/1040256 to your computer and use it in GitHub Desktop.
Save carlesso/1040256 to your computer and use it in GitHub Desktop.
class Post
include Mongoid::Document
include Mongoid::Paranoia
include Mongoid::Timestamps
field :title, :type => String
attr_accessor :tags_string
accepts_nested_attributes_for :answers
belongs_to :user
has_many :answers, :allow_destroy => true
has_and_belongs_to_many :tags
after_save :save_answers, :link_tags
def save_answers
answers.each do |a|
a.save
end
end
def link_tags
return if self.tags_string.nil?
self.update_attribute :tag_ids, tags_string.split(',').collect{|t| Tag.find_or_create_by(:t => t.downcase.strip).id}
self.reload
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment