I’m not sure if this is the best way to validate a foreign key in rails,
but I am using the following validation to do so (Rails 3, Rspec 2):
class Item < ActiveRecord::Base
validates :manufacturer_id, :inclusion => { :in =>
Manufacturer.all.collect { |m| m.id } }
end
While I can create a new valid Item record in the console with the code
above, it doesn’t work in my spec. The contents of my spec:
describe Item do
it “is valid when a manufacturer with manufacturer_id is in the
database” do
@manufacturer = Manufacturer.create!
@item = Item.new(:manufacturer_id => @manufacturer.id)
puts “VALIDATION ERRORS: #{@item.errors}”
puts “ID WE’RE CHECKING FOR IN THE DB: #{@item.manufacturer.id}”
puts “IDs THAT ARE IN THE DB: #{Manufacturer.all.collect { |m| m.id
}.to_s}”
@item.should be_valid
end
Which fails with:
Item
VALIDATION ERRORS: {:manufacturer_id=>[“is not included in the list”]}
ID WE’RE CHECKING FOR IN THE DB: 2
IDs THAT ARE IN THE DB: [1, 2]
is valid when a manufacturer with manufacturer_id is in the database
(FAILED - 1)
Failures:
- Item valid when manufacturer_id is in database
Failure/Error: @item.should be_valid
expected valid? to return true, got false./spec/models/item_spec.rb:47:in `block (2 levels) in <top
(required)>’
As you can see in the information I printed with the spec output, the ID
of the manufacturer is clearly in the array which the validation is
using to check inclusion. Any reason why this wouldn’t work in the test
env, but it would in development?
Thanks for your help!
Dave