How do i validate a column in one table against entries in other tables?
The code I used to validate that the entry for column “:product_id” of table “Preperiod” exists in the column “id” of table “Product” is
validate :product_id_exists
def product_id_exists
if Product.find_by_id(:product_id)==nil
errors.add(:base, "Product must be defined")
end
end
But now I get the error even when the product_id is right. What did I do wrong?
I use ruby 1.9.2 and rails 3.0.9
so you your code should look like:
or:
or: