Skip to content

Instantly share code, notes, and snippets.

@felipeorlando
Created September 12, 2016 18:11
Show Gist options
  • Save felipeorlando/34cc7139bb0e3bc746388564c5ea1dad to your computer and use it in GitHub Desktop.
Save felipeorlando/34cc7139bb0e3bc746388564c5ea1dad to your computer and use it in GitHub Desktop.
class Notice < ActiveRecord::Base
validates_presence_of :notice_category
has_many :notice_locales, :dependent => :destroy
accepts_nested_attributes_for :notice_locales, :allow_destroy => true
belongs_to :notice_category
has_one :notice_category_locale#, :foreign_key =>
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>", my_admin: "150x150>"}, default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
scope :all_notices, -> (locale_id) do
joins(:notice_locales, :notice_category_locale).
where('notice_locales.locale_id = ?', locale_id).
where('notice_category_locales.notice_category_id = notices.notice_category_id').
where('notice_category_locales.locale_id = ?', locale_id).
select('notice_category_locales.*')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment