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_and_belongs_to_many :subscribers, :class_name => "User", :join_table => "diary_entries_subscribers", :association_foreign_key => "subscriber_id"
9 scope :visible, -> { where(:visible => true) }
11 validates :title, :body, :presence => true
12 validates :title, :length => 1..255
13 validates :latitude, :allow_nil => true,
14 :numericality => { :greater_than_or_equal_to => -90,
15 :less_than_or_equal_to => 90 }
16 validates :longitude, :allow_nil => true,
17 :numericality => { :greater_than_or_equal_to => -180,
18 :less_than_or_equal_to => 180 }
19 validates :language, :user, :associated => true
21 after_save :spam_check
24 RichText.new(self[:body_format], self[:body])