Topic: ActiveRecord class find method returning very weird results
I have this problem with one of my controllers that is driving me absolutely bonkers. On the rendered page, I want to see all ingredients that I have inventory for. I am getting extraneous ingredients which I should not be seeing, as well as some ingredients that are missing. I have an Ingredient class, IngredientInventory class, and this code is in the RecipesController:
@all_ingredients = Ingredient.find(:all, :conditions => [ "company_id in (0, ?)", session[:company_id]], :order => "name")
@all_ingredients.each do |ing|
inventory = IngredientInventory.find(:first, :conditions => [ "ingredient_id = ? and company_id in (0, ?)", ing.id, session[:company_id]])
@all_ingredients.delete ing if !inventory
I'm using 1.16 with Mongrel, and the queries printed in the log all seem correct. If I change the find to find_by_sql("select * from ingredients inner join ingredient_inventories on (ingredients.id = ingredient_inventories.ingredient_id), it works. I have double- and triple-checked the records in the database.
I'm hoping somebody can post some clues, because this is driving me nuts! I'm thinking AR might be doing something funky behind the scenes, but I don't know what, or how or why...