Created
June 22, 2011 14:52
-
-
Save carlesso/1040256 to your computer and use it in GitHub Desktop.
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
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