I have simple model:
class Category < ActiveRecord::Base
belongs_to :board
validates :name, presence: true, uniqueness: {scope: :board_id}
validates :board, presence: true
validates :display_order, presence: true, uniqueness: {scope: :board_id}
before_save :set_display_order
private
def set_display_order
last = self.board.categories.order("display_order DESC").last
self.display_order = last.display_order + 100 if last
end
end
When I added this before_save callback, these tests start to fail:
it { should validate_uniqueness_of(:display_order).scoped_to(:board_id) }
it { should validate_uniqueness_of(:name).scoped_to(:board_id) }
With error (this if for the line in private method last = ...
):
NoMethodError:
undefined method `categories' for nil:NilClass
Other shoulda tests work fine:
it { should validate_presence_of(:name) }
it { should validate_presence_of(:board) }
it { should belong_to :board }
Any idea what is problem here? I tried to change before_save
to before_validation
but still the same.