I have an empty table called users. I create a new user via:
User.create :fb_id => fb_id
It successfully creates an entry.
Then I query it via:
User.first(:include => :cookies, :conditions => {:cookies => {:opened => false} , :fb_id => fb_id})
returns nil!!
I check the table and the user is there.
So I create a new, duplicate user via:
User.create :fb_id => fb_id
Now there’s exact two row with same fb_id. If I query now just like before:
User.first(:include => :cookies, :conditions => {:cookies => {:opened => false} , :fb_id => fb_id})
it returns the new user now with id 2!
What’s going on? I’ve been trying to debug it for 3-4 hours but can’t figure it out.
Thanks
My Models:
class User < ActiveRecord::Base
has_many :cookies
has_many :fortunes, :through => :cookies
end
class Cookie < ActiveRecord::Base
belongs_to :user
belongs_to :fortune
end
class Fortune < ActiveRecord::Base
has_many :cookies
has_many :users, :through => :cookies
end
I believe that Rails will use a
left outer joinwhen usinginclude. However, when joining, the associated row, all values will benull. Therefore, you should use something like: