Skip to content
Andrew Geweke edited this page Nov 2, 2013 · 2 revisions

Validations under low_card_tables basically change not at all. There are two ways you can validate:

On the Referring Model

This is what you had before you moved to low_card_tables, and it continues to work identically:

class User < ActiveRecord::Base
  has_low_card_table :status

  validates :gender, :inclusion => { :in => %w{male female other} }
end

This works exactly as it would were you not using low_card_tables at all.

The only case where you'll run into problems here is if the methods in question aren't automatically delegated to the low-card model. (See delegation for more information.) In that case, ActiveRecord doesn't think you have a column called, e.g., gender, so validating on it won't work. You have two options: either add the validation to the low-card model directly (see below), or simply write it as a custom validation block that tests against, e.g., user.status.gender.

On the Low-Card Model

You can actually add validations directly to the low-card model itself, too, if you want. Just like any validations, if they fail, the row will not be created. Typically this will happen when you attempt to save the referring model, and so you'll get an ActiveRecord::RecordInvalid exception thrown just as you'd expect.

class UserStatus < ActiveRecord::Base
  is_low_card_table

  validates :gender, :inclusion => { :in => %w{male female other} }
end

This can be useful either when certain methods aren't delegated to the low-card table, or when you're sharing a single low-card model among many referring tables and want the same validations to be applied to all of those attributes.

Clone this wiki locally