Rails: limit enum column for already submitted values -
in rails app have 2 models: post , post_translations.
class posttranslation < activerecord::base belongs_to :post languages = %w( en fr es de ) validates_inclusion_of :language, :in => languages end class post < activerecord::base has_many :post_translations end
i want prevent same language translation being submitted twice, want limit enums values not listed in language column of particular post_id.
don't know if should in model, controller or helper.
best practice?
thanks in advance.
i'd use attribute on class instead of defining on instance.
class posttranslation < activerecord::base @@languages = %w( en fr es de ) cattr_reader :languages belongs_to :post validates :language, :inclusion => { :in => @@languages }, :uniqueness => { :scope => :post_id } end
now fulfill requirement of showing languages without translations, define method on post:
class post < activerecord::base has_many :post_translations def untranslated posttranslation.languages - post_translations.map(&:language) end end
then can build select menu getting post (@post = post.find(params[:id]
) , populating collection @post.untranslated
.
Comments
Post a Comment