1 class DiaryEntry < ActiveRecord::Base
2 belongs_to :user, :counter_cache => true
3 belongs_to :language, :foreign_key => "language_code"
5 has_many :comments, -> { order(:id).preload(:user) }, :class_name => "DiaryComment"
6 has_many :visible_comments, -> { joins(:user).where(:visible => true, :users => { :status => %w(active confirmed) }).order(:id) }, :class_name => "DiaryComment"
7 has_many :subscriptions, :class_name => "DiaryEntrySubscription"
8 has_many :subscribers, :through => :subscriptions, :source => :user
10 scope :visible, -> { where(:visible => true) }
12 validates :title, :body, :presence => true
13 validates :title, :length => 1..255
14 validates :latitude, :allow_nil => true,
15 :numericality => { :greater_than_or_equal_to => -90,
16 :less_than_or_equal_to => 90 }
17 validates :longitude, :allow_nil => true,
18 :numericality => { :greater_than_or_equal_to => -180,
19 :less_than_or_equal_to => 180 }
20 validates :language, :user, :associated => true
22 after_save :spam_check
25 RichText.new(self[:body_format], self[:body])