Ian Moody wrote in post #1110472:> 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 very interesting. The 3.2 version "feels" more correct to me,
but I haven''t fully thought it through.
> 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?
My guess is that the change wasn''t arbitrary. I''m pretty sure
the change
was made for a reason that made sense to the core team at the time.
That being said, unless I''m missing something really obvious I would
have used a polymorphic association here:
class Link < ActiveRecord::Base
belongs_to :linkable, :polymorphic => true
end
class Product < ActiveRecord::Base
has_many :links, :as => :linkable
end
class Content < ActiveRecord::Base
has_many :links, :as => :linkable
end
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
--
Posted via http://www.ruby-forum.com/.
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/af2e8b5375faf560ae04cb1c6277ae1c%40ruby-forum.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.