开发者

ActiveRecord returns model objects with un-initialized associations

I have a simple join query which in some cases returns ActiveRecord objects with uninitialized associations, and I try to understand why. (My setup: rails 2.3.8 with MySQL)

Here are my models:

class Member
has_many :twitter_status_relations
 //has some more unrelated associations
end

class TwitterStatus
  has_many :twitter_status_rela开发者_开发知识库tions 
end

class TwitterStatusRelation
 belongs_to :member
 belongs_to :twitter_status
end

And here is the query I perform:

 result = TwitterStatusRelation.all(:joins => :twitter_status, 
:conditions=>{:twitter_statuses=>{:sent_at=>1.month.ago..DateTime.now}}, :include=>:member,:group=>"twitter_status_relations.member_id")

Now, when I run in it the first time in the app, it works fine:

print result[0].member, result[0].member.class.reflect_on_all_associations(:has_many)
#=> <Member...>, [<ActiveRecord::Reflection::AssociationReflection,...]

BUT, when I run it again, and try accessing any association of the member, I get nil exception. Print shows the following:

print result[0].member, result[0].member.class.reflect_on_all_associations(:has_many)
#=> <Member...>, [-- empty ---]

Looks like the member object doesn't have any associations, and so when I try to access any of it, I get an exception.

Do you have any idea why ActiveRecord wouldn't initialize associations of the returned objects in some cases? I would appreciate any half-idea because I'm stuck.


Here is the SQL the above query produces (Ran posted the question on my behalf). The SQL has more fields then the query, because I simplified the query when posting it, removing conditions that aren't relevant to the problem.

SELECT `twitter_status_relations`.`id` AS t0_r0,

twitter_status_relations.twitter_status_id AS t0_r1, twitter_status_relations.source_twitter_identity_id AS t0_r2, twitter_status_relations.relation_type AS t0_r3, twitter_status_relations.relation_data AS t0_r4, twitter_status_relations.linked_twitter_identity_id AS t0_r5, twitter_status_relations.user_id AS t0_r6, twitter_status_relations.linked_member_id AS t0_r7, members.id AS t1_r0, members.user_id AS t1_r1, members.name AS t1_r2, members.email AS t1_r3, members.member_rating AS t1_r4, members.created_at AS t1_r5, members.updated_at AS t1_r6, members.merged_with_member_id AS t1_r7, members.engage_rating AS t1_r8, members.support_rating AS t1_r9, members.user_engage_rating AS t1_r10, members.user_support_rating AS t1_r11, members.influence_rating AS t1_r12, members.twitter_username AS t1_r13, members.lead_rating AS t1_r14, members.follow_rating AS t1_r15, members.unfollow_rating AS t1_r16, members.followers_count AS t1_r17, members.hidden AS t1_r18 FROM twitter_status_relations LEFT OUTER JOIN members ON members.id = twitter_status_relations.linked_member_id WHERE (twitter_status_relations.user_id = 1 AND twitter_status_relations.relation_type IN( 'mention','reply','received_dm','retweet','link','term','hashtag' ) AND twitter_status_relations.linked_member_id IN( 83995,128457,21421,138316,128455,97475,128453,436231,82236,441208,138564,138337,436223,436222,441093,21194,441088,441092,438998,442752,138331,138327,138325,444897,9277,12,509521,13,15,534511,7606,7447,200,7,4,17200,5,652302,1,5536,18770,652301,214082,150870,436228,81204,436225,662513,138608,138338 )) AND twitter_status_relations.id IN (8304, 26493, 113492, 113638, 1, 6, 41213, 113493, 20, 26173) GROUP BY twitter_status_relations.linked_member_id ORDER BY members.member_rating

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜