ruby on Rails 活动记录垃圾收集问题(ruby 1.8.7 和rails 2.3.5)
我有以下实体:品牌、评分记录。品牌拥有多项评分记录。
如果我有一个正在处理这样的品牌的块:
brands.each{|brand|
# do some stuff
brand.do_some_stuff
some_scoring_records = ScoringRecords.find(:all,:conditions => ["computed_date = ?",today], :order => 'brand.id' )
# do some more stuff
brand.do_some_more_stuff(brand)
brand.do_even_more_stuff(brand)
}
问题1:当我像这样加载scoring_records时,它是否会在rails中自动将其与品牌关联(或者是否等待关系被行使。换句话说在做更多事情时,如果我调用brand.scoring_records,评分记录仍然存在,它会调用数据库吗?或者有资格进行垃圾收集?(我猜它们不是,因为品牌与scoring_records有关系,所以从那时起品牌是稍后引用,并且不符合资格,有没有办法在没有关联的情况下加载这些记录?
问题2:如果记录不会被gc'd,是否有办法使scoring_records符合gc资格,但是保留品牌及其其余关系?我不想删除评分记录,我只想将它们从内存中删除。
I have the following entities : brand, scoring_records. Brand has_many scoring_records.
If I have a block where I am working on a brand like this:
brands.each{|brand|
# do some stuff
brand.do_some_stuff
some_scoring_records = ScoringRecords.find(:all,:conditions => ["computed_date = ?",today], :order => 'brand.id' )
# do some more stuff
brand.do_some_more_stuff(brand)
brand.do_even_more_stuff(brand)
}
question 1: When I load scoring_records like this, does it associate it with the brand automatically in rails (or does it wait for the relationship to be exercised. In other words in do some more stuff are the scoring records still around if I call brand.scoring_records, will it call to the database?, or eligible for garbage collection? (Im guessing they are not, because the brand has the relation to scoring_records, so since brand is referenced later, and not eligible, neither are any of the scoring_records. Is there a way to load these without the association?
question 2: If the records will not be gc'd, is there anyway to make the scoring_records eligible for gc, but keep the brand around, and the rest of its relationships? I dont want to delete the scoring_records, I just want to remove them from memory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题1:scoring_records是一个局部变量,在do_some_more_stuff中不可用。 has_many 添加的scoring_records 不是一个变量,它是一个方法,你不能给它赋值。您需要阅读变量范围。这是一个好的开始:http://www.techotopia.com/index.php/Ruby_Variable_Scope。您提供的代码中的scoring_record变量对brand变量中的scoring_records没有影响。
如果此代码位于品牌类中,它会将查找返回的评分记录分配给关联,立即将其关联,但仅在保存时保留。不是针对品牌变量,而是针对对象本身。
问题2:scoring_records与品牌无关,其范围是区块。
Question 1: scoring_records is a local variable that is not available in do_some_more_stuff. The scoring_records that is added by has_many is not a variable, it's a method, and you cannot assign to it. You need to read up on variable scopes. This is a good start: http://www.techotopia.com/index.php/Ruby_Variable_Scope. The scoring_record variable in the code you have supplied has no effect on the scoring_records in the brand variable.
If this code were in the brand class, it would assign the scoring records returned by the find to the association, associating it immediately, but persisting only when saved. Not for the brand variable, but for the object itself.
Question 2: scoring_records aren't related to brand, its scope is the block.