lundi 30 mai 2016

includes called on a relation in AR. Does it scope on the relation or the entire database?

I have an odd situation where an has_many association yields me an empty collection but a scope on an empty collection yields me a record. Any idea what is going on?

class Person
  has_many :apples

end

Class Apple
  belongs_to :person

  scope :rotten, -> { includes(pit: :seed).where(seeds: { seed_type_id: Seedtype.edible.id})
end

Inside the Person model:

=> Apple.all
[
    [0] #<Apple:0x007fdff8b49b70> {
                           :id => 1,
                      :user_id => 1,
    }
]

=> apples
[]

=> apples.rotten
[
    [0] #<Apple:0x007fdff8b49b70> {
                           :id => 1,
                      :user_id => 1
    }
]

Any ideas as to what is going on?

Aucun commentaire:

Enregistrer un commentaire