1

これらの ActiveRecord モデルがあるとしましょう:

class User < ActiveRecord::Base
  has_many :emotions
end

class Emotion < ActiveRecord::Base
  belongs_to :user
end

そして、私はこのコードを持っています:

user1 = User.where(id: 1).includes(:emotions).first
user2 = User.where(id: 1).first

と を区別する方法はありuser1.emotionsますuser2.emotionsか? リレーションが熱心にロードされているか、データベースからのクエリをまだ待っているかを知るために呼び出すことができるメソッドはありますか?

4

1 に答える 1

2

ActiveRecord のソース コードを調べればよかったのに。

user1.emotions.loaded? # => true
user2.emotions.loaded? # => false

#loaded?私が探していたものです。

于 2013-10-10T18:03:01.917 に答える