I have a has many through relationship in my rails app where the uniqueness of the association is validated like so:
class Foo < ActiveRecord::Base
...
has_many :foo_bars
has_many :bars, :through => :foo_bars, :uniq => true
validates_associated :foo_bars
...
end
And this is working great, but what I want to do in my controller is find out if when I try to create a new association between existing foo and bar if it was successfull. Something like this:
if @myFoo.bars << @bar
...
end
but that doesn’t work because the << method returns an array of all of myFoo’s Bars not true/false. I know there must be a correct ‘rails way’ to do this but I don’t know what it is (the only thing I can think of is to check if the number of associated bars changed after the query but that seems really sloppy). Any suggestions?
Try this: