RSpec, validates_presence_of, and foreign_key

I'm having trouble with validation and RSpec/spec::rails.

I have an Item model which acts_as_ratable. Item has_many :ratings (Rating is another model which essentially maps users to ratings). In the Rating model I have

belongs_to :item, :foreign_key => 'rateable_id' validates_presence_of :rateable_id

In the Rails documentation it says to validate on :rateable_id rather than :rateable, so this is what I've done above.

Now, in my specs I have problems. If I do something like

@rating = Rating.new(...) # an invalid Rating @rating.valid? @rating.errors.on(:rateable_id).should_not be_nil

(to ensure that an invalid Rating cant be saved), it fails (the validation is ineffectual).

If I change back to

validates_presence_of :rateable

and check for errors.on.(:ratable), that spec will work. However that causes another problem. In that case, if I do this

@rating = Rating.new(...) # a valid Rating @rating.save!

(which I am doing to test some after_save hooks) it fails and says Rateable cannot be blank.

I'm quite confused. I think its related to the foreign_key because I'm not having trouble with any of the other validations. Can anyone shed some light on the situation?

Thanks.