Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Activerecord: how to write a new feature?

Received: by 10.236.184.137 with SMTP id s9mr783378yhm.43.1344570276382;
        Thu, 09 Aug 2012 20:44:36 -0700 (PDT)
X-BeenThere: rubyonrails-core@googlegroups.com
Received: by 10.236.113.178 with SMTP id a38ls6582352yhh.9.gmail; Thu, 09 Aug
 2012 20:44:31 -0700 (PDT)
Received: by 10.236.142.211 with SMTP id i59mr426195yhj.1.1344570271156;
        Thu, 09 Aug 2012 20:44:31 -0700 (PDT)
Date: Thu, 9 Aug 2012 20:44:30 -0700 (PDT)
From: Rafael Almeida <almeida...@gmail.com>
To: rubyonrails-core@googlegroups.com
Message-Id: <d1e6e00d-35ce-4246-9193-2bfc24055887@googlegroups.com>
In-Reply-To: <CAPeiConKbiPGswvLW6UDd+d9VzX9S2djyr8N0XRRFb85CtKtpg@mail.gmail.com>
References: <26872021-da32-4ca1-8ff7-acb2da38a788@googlegroups.com> <CABjg+5ves3EW95KO_wLFMu1EPjc8-p6G+boPLCChaGcA62ZXgA@mail.gmail.com>
 <CAPeiConKbiPGswvLW6UDd+d9VzX9S2djyr8N0XRRFb85CtKtpg@mail.gmail.com>
Subject: Re: [Rails-core] Activerecord: how to write a new feature?
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_174_24066030.1344570270541"

------=_Part_174_24066030.1344570270541
Content-Type: multipart/alternative; 
	boundary="----=_Part_175_12649898.1344570270541"

------=_Part_175_12649898.1344570270541
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Unless I misunderstood you, I think that would only work if there were only 
2 possible service types and I grouped by person_id. I have something a 
little more general in mind.

On Thursday, August 9, 2012 9:26:24 AM UTC-3, EMoreth wrote:
>
> Isn't the case where a simple "group by X having count(*) = 2" could 
> handle the problem ?
>
> Arel can achieve this query easily.
>
> On Wed, Aug 8, 2012 at 2:27 PM, Pedro Nascimento <pnasc...@gmail.com<javascript:>
> > wrote:
>
>> Take a look at ARel and Squeel gem. Squeel gem supports something like 
>> this already.
>>
>> On Wed, Aug 8, 2012 at 12:05 PM, Rafael Almeida <almei...@gmail.com<javascript:>
>> > wrote:
>>
>>> Hello.
>>>
>>> Let's say I have the following schema
>>>
>>>     People <---> people_services <---> services
>>>
>>> That is, people has a many-to-many relationship with services. Say I 
>>> want to find all people who has services of types 1 and 2. How can we do it 
>>> on activerecord today?
>>>
>>> My best solution so far is this
>>>
>>>     people_query = Person.joins(:services)
>>>     people = []
>>>     [1,2].each do |t|
>>>         people << people_query.where(:type => 1)
>>>     end
>>>     people = people.reduce(:&)
>>>
>>> That's not so good because by doing reduce(:&) you're exiting 
>>> activerecord domain. So you wouldn't be able to do something like 
>>> people.reduce(:&).limit(10). Also, that way I'm loading a lot more data 
>>> than I actually need in my application memory. So, certainly not optimal 
>>> solution.
>>>
>>> It's possible to make such query in SQL alone, if the dbms supports 
>>> INTERSECTION then doing it is straightforward. If it doesn't, it's still 
>>> possible using joins on subqueries.
>>>
>>> I think activerecord ought support such query and I'm willing to take my 
>>> time and write the code for it. However, I'd like someone to help me out 
>>> with it a bit. I don't understand rails or activerecord code base so well. 
>>> Anyway, it would be best if I did a gem that introduces that feature into 
>>> activerecord. Can anyone tell me what should I look up? A high level 
>>> description of what I must do would be great. Something like "You'll need 
>>> to create a gem, monkey patch this activerecord class, probably use this 
>>> and this function to help you writing your SQL" would be nice.
>>>
>>> Cheers
>>>
>>> -- 
>>> 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/-/jz3f6lJrXEkJ.
>>> To post to this group, send email to rubyonra...@googlegroups.com<javascript:>
>>> .
>>> To unsubscribe from this group, send email to 
>>> rubyonrails-co...@googlegroups.com <javascript:>.
>>> For more options, visit this group at 
>>> http://groups.google.com/group/rubyonrails-core?hl=en.
>>>
>>
>>  -- 
>> 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 rubyonra...@googlegroups.com<javascript:>
>> .
>> To unsubscribe from this group, send email to 
>> rubyonrails-co...@googlegroups.com <javascript:>.
>> For more options, visit this group at 
>> http://groups.google.com/group/rubyonrails-core?hl=en.
>>
>
>
>
> -- 
> Att,
> Everton
> http://www.evertonmoreth.com.br
>
>
------=_Part_175_12649898.1344570270541
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

Unless I misunderstood you, I think that would only work if there were only 2 possible service types and I grouped by person_id. I have something a little more general in mind.<br><br>On Thursday, August 9, 2012 9:26:24 AM UTC-3, EMoreth wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Isn't the case where a simple "group by X having count(*) = 2" could handle the problem ?<br><br>Arel can achieve this query easily.<br><br><div class="gmail_quote">On Wed, Aug 8, 2012 at 2:27 PM, Pedro Nascimento <span dir="ltr">&lt;<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Dy8y-duBByIJ">pnasc...@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Take a look at ARel and Squeel gem. Squeel gem supports something like this already.<div><div>

<div><br><div class="gmail_quote">On Wed, Aug 8, 2012 at 12:05 PM, Rafael Almeida <span dir="ltr">&lt;<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Dy8y-duBByIJ">almei...@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello.<div><br></div><div>Let's say I have the following schema</div><div><br></div><div>&nbsp; &nbsp; People &lt;---&gt; people_services &lt;---&gt; services</div>



<div><br></div><div>That is, people has a many-to-many relationship with services. Say I want to find all people who has services of types 1 and 2. How can we do it on activerecord today?</div><div><br></div><div>My best solution so far is this</div>



<div><br></div><div>&nbsp; &nbsp; people_query = Person.joins(:services)</div><div>&nbsp; &nbsp; people = []</div><div>&nbsp; &nbsp; [1,2].each do |t|</div><div>&nbsp; &nbsp; &nbsp; &nbsp; people &lt;&lt; people_query.where(:type =&gt; 1)</div><div>&nbsp; &nbsp; end</div><div>&nbsp; &nbsp; people = people.reduce(:&amp;)</div>



<div><br></div><div>That's not so good because by doing reduce(:&amp;) you're exiting activerecord domain. So you wouldn't be able to do something like people.reduce(:&amp;).limit(10). Also, that way I'm loading a lot more data than I actually need in my application memory. So, certainly not optimal solution.</div>



<div><br></div><div>It's possible to make such query in SQL alone, if the dbms supports INTERSECTION then doing it is straightforward. If it doesn't, it's still possible using joins on subqueries.</div><div><br>



</div><div>I think activerecord ought support such query and I'm willing to take my time and write the code for it. However, I'd like someone to help me out with it a bit. I don't understand rails or activerecord code base so well. Anyway, it would be best if I did a gem that introduces that feature into activerecord. Can anyone tell me what should I look up? A high level description of what I must do would be great. Something like "You'll need to create a gem, monkey patch this activerecord class, probably use this and this function to help you writing your SQL" would be nice.</div>



<div><br></div><div>Cheers</div><span><font color="#888888">

<p></p>

-- <br>
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.<br>
To view this discussion on the web visit <a href="https://groups.google.com/d/msg/rubyonrails-core/-/jz3f6lJrXEkJ" target="_blank">https://groups.google.com/d/<wbr>msg/rubyonrails-core/-/<wbr>jz3f6lJrXEkJ</a>.<br> 
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Dy8y-duBByIJ">rubyonra...@googlegroups.<wbr>com</a>.<br>
To unsubscribe from this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Dy8y-duBByIJ">rubyonrails-co...@<wbr>googlegroups.com</a>.<br>

For more options, visit this group at <a href="http://groups.google.com/group/rubyonrails-core?hl=en" target="_blank">http://groups.google.com/<wbr>group/rubyonrails-core?hl=en</a>.<br>


</font></span></blockquote></div><br></div>

<p></p>

-- <br>
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.<br>
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Dy8y-duBByIJ">rubyonra...@googlegroups.<wbr>com</a>.<br>
To unsubscribe from this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Dy8y-duBByIJ">rubyonrails-co...@<wbr>googlegroups.com</a>.<br>

For more options, visit this group at <a href="http://groups.google.com/group/rubyonrails-core?hl=en" target="_blank">http://groups.google.com/<wbr>group/rubyonrails-core?hl=en</a>.<br>


</div></div></blockquote></div><br><br clear="all"><br>-- <br><div>Att,</div>Everton<br><a href="http://www.evertonmoreth.com.br" target="_blank">http://www.evertonmoreth.com.<wbr>br</a><br><br>
</blockquote>
------=_Part_175_12649898.1344570270541--

------=_Part_174_24066030.1344570270541--