开发者

Rails 3.1, has_many relationship and new records problem

Supose I have a simple has_many relationship betwen model_1 and model_2:

class Model1 < ActiveRecord::Base
    has_many :models_2
end

class Model2 < ActiveRecord::Base
    belongs_to :model_1
end

Now, I create an instance of Model2 through the relationsh开发者_如何学Goip:

irb>m1 = Model1.first
irb>m2 = m1.models_2.new

Now, if I want to ask for the size of the relationship, there's a huge difference between Rails 3.0.x and Rails 3.1.

Following the previous example, in Rails 3.0.x i get:

irb>m1.models_2.any?
irb>false
irb>m1.models_2.size
irb>0

This means, new objects are not being considered

This exact same query in Rails 3.1:

irb>m1.models_2.any?
irb>true #OMG!!
irb>m1.models_2.size
irb>1

So, the solution is:

irb>m1.models_2.all.any?
irb>false
irb>m1.models_2.all.size
irb>0

If I have to change ALL my relationships with .all, I'm in a big trouble... any ideas about this situation? Thank you very much.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜