so basically i has this setup (example):
class FellowUser < ActiveRecord::Base
belongs_to :fellow
belongs_to :user
end
class Fellows < ActiveRecord::Base
belongs_to :user
has_many :fellow_users
has_many :fellows, :through => :fellow_users
end
what i want, in rspec terms, is this:
i want validation to fire if i associate user twice to fellow
it “should not be able to be fellow twice” do
@fellow.fellows << users(:george)
@fellow.fellows << users(:quentin)
@ft.fellows.size.should == 2
# same user second time
@fellow.fellows << users(:george)
# tried @fellow.should_not be_valid - not working either
@fellow.should have(1).error_on(:fellows)
@fellow.fellows.size.should == 2
end
in join model:
i tried this with no luck:
validates_uniqueness_of :fellow_id, :scope => :user_id
i tried this with same result:
validate do |record|
# add_to_base, can be add(:fellows, --//–) and no luck here
record.errors.add_to_base(“duplicating hitcher”) if
record.fellow.fellows.size!=record.fellow.fellows.uniq.size
end
in parent model:
this makes model not valid but it still saves and record is add to
join model
validate do |record|
record.errors.add(:fellows, "you can't be fellow twice") if
record.fellows.size!=record.fellows.uniq.size
end
Any suggestions will be greatly appreciated.