Ruby on Rails Tuesday, May 28, 2013

Can someone tell me I'm not going mad.


We have a very large rails app, with lots of things like:

# Link model
belongs_to :product
belongs_to :content
belongs_to :member
belongs_to :boat

# Product model
has_many :links, :foreign_key => :product_id

# Content model
has_many :links, :foreign_key => :content_id

# Test
Product.new.links.all

If we run this test on Rails 3.0 we get:

SELECT `links`.* FROM `links` WHERE (`links`.product_id = NULL)

If we run this test on Rails 3.2 we get:

SELECT `links`.* FROM `links` WHERE `links`.`product_id` IS NULL

Notice the VERY BIG change in how Rails check for NULL (it may not look like a big change, but it is).

In the first case (3.0) we get NO records, as you would expect on a new record, and as everything in our code assumes.

In the second case (3.2) we get ALL RECORDS that have an empty product_id field.

This is obviously quite a change.

We use our 'links' table to store many kinds of relationships between objects in our system.

So for a link between a 'Member' and a 'Boat', the product_id field would be blank and just the member_id and boat_id would be filled in.

Under rails 3.0 this all works fine.

Under rails 3.2, EVERY SINGLE use of has_many / has_many :through now returns ALL unrelated records for every query on a new record, simply because of the change in NULL checking.


ps. caps used for highlighting, not shouting.


Does anyone know if this is expected behaviour?



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bec0dae9-1871-4bed-a221-6e7317fe8442%40googlegroups.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment