zdennis
2012-Aug-31 20:08 UTC
Feature Request: Auto-infer .includes from .where with ActiveRecord queries
In the following snippet, can''t the includes being inferred from the
where
clause?
Order.includes(:items).where(:items => { :category => [1,2,3] })
Can''t the includes be inferred from the where clause?
Order.where(:items => { :category => [1,2,3] })
Would this conflict with any existing usages of the where clause and
hashes?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-core/-/ZnMMdZu9lLoJ.
To post to this group, send email to rubyonrails-core@googlegroups.com.
To unsubscribe from this group, send email to
rubyonrails-core+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.
Matt Jones
2012-Aug-31 20:56 UTC
Re: Feature Request: Auto-infer .includes from .where with ActiveRecord queries
On Aug 31, 2012, at 4:08 PM, zdennis wrote:> In the following snippet, can''t the includes being inferred from the where clause? > > Order.includes(:items).where(:items => { :category => [1,2,3] }) > > Can''t the includes be inferred from the where clause? > > Order.where(:items => { :category => [1,2,3] }) > > Would this conflict with any existing usages of the where clause and hashes? >The hazard with auto-inferring here (IMHO) is that it makes forgetting a joins clause go from a runtime error to a silent logic error. If I *meant* to build this chain: Order.joins(:items).where(:items => { :category => [1,2,3] }) but forgot the joins, I''ve now got a query that returns *all* Orders. Not a big deal in this case, but could be a recipe for disaster if the conditions in the where are for access control... --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Matt Jones
2012-Sep-01 14:03 UTC
Re: Feature Request: Auto-infer .includes from .where with ActiveRecord queries
On Aug 31, 2012, at 4:56 PM, Matt Jones wrote:> > On Aug 31, 2012, at 4:08 PM, zdennis wrote: > >> In the following snippet, can''t the includes being inferred from the where clause? >> >> Order.includes(:items).where(:items => { :category => [1,2,3] }) >> >> Can''t the includes be inferred from the where clause? >> >> Order.where(:items => { :category => [1,2,3] }) >> >> Would this conflict with any existing usages of the where clause and hashes? >> > > The hazard with auto-inferring here (IMHO) is that it makes forgetting a joins clause go from a runtime error to a silent logic error. If I *meant* to build this chain: > > Order.joins(:items).where(:items => { :category => [1,2,3] }) > > but forgot the joins, I''ve now got a query that returns *all* Orders. Not a big deal in this case, but could be a recipe for disaster if the conditions in the where are for access control...Ugh, posting before testing - this actually only happens if you have a condition in the where that evaluates to true for NULLs. I still think the overall criticism stands, however. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Everton Moreth
2012-Sep-25 16:07 UTC
Re: Feature Request: Auto-infer .includes from .where with ActiveRecord queries
I believe it cant be inferred, thats why:
Order.where(:items => {:name => "X"})
SELECT `orders`.* FROM `orders` WHERE `orders`.`name` = ''X''
<== Clearly
wrong.
Order.*includes(:item)*.where(:items => {:name => "X"})
SELECT `orders`.`id` AS t0_r0, `orders`.`item_id` AS t0_r1, ... ,
`items`.`id` AS t1_r0, `items`.`name` AS t1_r1, ... FROM `orders` LEFT
OUTER JOIN `items` ON `items`.`id` = `orders`.`item_id` WHERE
`orders`.`name` = ''X''
Selects every column from Order and Item using *LEFT OUTER JOIN**
*Order.*joins(:item)*.where(:items => {:name => "X"})
SELECT `orders`.* FROM `orders` INNER JOIN `items` ON `items`.`id`
`orders`.`item_id` WHERE `items`.`name` = ''X''
*INNER JOIN*s selection without selecting every field*
*Order.*includes(:item).joins(:item)*.where(:items => {:name =>
"X"})
SELECT `orders`.`id` AS t0_r0, `orders`.`item_id` AS t0_r1, ... ,
`items`.`id` AS t1_r0, `items`.`name` AS t1_r1, ... FROM `orders` INNER
JOIN `trackers` ON `trackers`.`id` = `issues`.`tracker_id` WHERE
`trackers`.`name` = ''X''
*INNER JOIN*s selection *selecting* every field
Each options have its own behaviour, and is suitable for one case.
Everton
On Sat, Sep 1, 2012 at 11:03 AM, Matt Jones <al2o3cr@gmail.com> wrote:
>
> On Aug 31, 2012, at 4:56 PM, Matt Jones wrote:
>
> >
> > On Aug 31, 2012, at 4:08 PM, zdennis wrote:
> >
> >> In the following snippet, can''t the includes being
inferred from the
> where clause?
> >>
> >> Order.includes(:items).where(:items => { :category =>
[1,2,3] })
> >>
> >> Can''t the includes be inferred from the where clause?
> >>
> >> Order.where(:items => { :category => [1,2,3] })
> >>
> >> Would this conflict with any existing usages of the where clause
and
> hashes?
> >>
> >
> > The hazard with auto-inferring here (IMHO) is that it makes forgetting
a
> joins clause go from a runtime error to a silent logic error. If I *meant*
> to build this chain:
> >
> > Order.joins(:items).where(:items => { :category => [1,2,3] })
> >
> > but forgot the joins, I''ve now got a query that returns *all*
Orders.
> Not a big deal in this case, but could be a recipe for disaster if the
> conditions in the where are for access control...
>
> Ugh, posting before testing - this actually only happens if you have a
> condition in the where that evaluates to true for NULLs. I still think the
> overall criticism stands, however.
>
> --Matt Jones
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To post to this group, send email to rubyonrails-core@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-core+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-core?hl=en.
>
>
--
Att,
Everton
http://www.evertonmoreth.com.br
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com.
To unsubscribe from this group, send email to
rubyonrails-core+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.