Re: [Rails-core] Activerecord: how to write a new feature?

2012-08-14 Thread Rodrigo Rosenfeld Rosas
Em 13-08-2012 21:49, Rafael Almeida escreveu: On Sunday, August 12, 2012 12:06:00 AM UTC-3, Matt jones wrote: On Aug 11, 2012, at 6:23 PM, Rafael Almeida wrote: On Friday, August 10, 2012 5:50:58 PM UTC-3, EMoreth wrote: Try this:

Re: [Rails-core] Activerecord: how to write a new feature?

2012-08-13 Thread Rafael Almeida
On Sunday, August 12, 2012 12:06:00 AM UTC-3, Matt jones wrote: On Aug 11, 2012, at 6:23 PM, Rafael Almeida wrote: On Friday, August 10, 2012 5:50:58 PM UTC-3, EMoreth wrote: Try this: Project.joins(:services).where(:services = { :id = [1,2] }).group(:id).having(count(*) =

Re: [Rails-core] Activerecord: how to write a new feature?

2012-08-12 Thread Jonathan Lozinski
On Aug 11, 2012, at 6:23 PM, Rafael Almeida wrote: On Friday, August 10, 2012 5:50:58 PM UTC-3, EMoreth wrote: Try this: Project.joins(:services).where(:services = { :id = [1,2] }).group(:id).having(count(*) = 2).all This produces to me: SELECT `projects`.* FROM `projects` INNER JOIN

Re: [Rails-core] Activerecord: how to write a new feature?

2012-08-11 Thread Rafael Almeida
On Friday, August 10, 2012 5:50:58 PM UTC-3, EMoreth wrote: Try this: Project.joins(:services).where(:services = { :id = [1,2] }).group(:id).having(count(*) = 2).all This produces to me: SELECT `projects`.* FROM `projects` INNER JOIN `services` ON `services`.`project_id` =

Re: [Rails-core] Activerecord: how to write a new feature?

2012-08-11 Thread Matt Jones
On Aug 11, 2012, at 6:23 PM, Rafael Almeida wrote: On Friday, August 10, 2012 5:50:58 PM UTC-3, EMoreth wrote: Try this: Project.joins(:services).where(:services = { :id = [1,2] }).group(:id).having(count(*) = 2).all This produces to me: SELECT `projects`.* FROM `projects` INNER

Re: [Rails-core] Activerecord: how to write a new feature?

2012-08-10 Thread Everton Moreth
Try this: Project.joins(:services).where(:services = { :id = [1,2] }).group(:id).having(count(*) = 2).all This produces to me: SELECT `projects`.* FROM `projects` INNER JOIN `services` ON `services`.`project_id` = `projects`.`id` WHERE `services`.`id` IN (1, 2) GROUP BY id HAVING count(*) = 2

Re: [Rails-core] Activerecord: how to write a new feature?

2012-08-09 Thread Everton Moreth
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 pnascime...@gmail.comwrote: Take a look at ARel and Squeel gem. Squeel gem supports something like this already. On

Re: [Rails-core] Activerecord: how to write a new feature?

2012-08-09 Thread Rafael Almeida
I studied Squeel a bit, the best I could do was this: Person.joins{services}.where{ id.in(Person.joins{services}.where{services.type == 1}.select{id}) id.in(Person.joins{services}.where{services.type == 2}.select{id})} which is both unefficient and very ugly. I think it would

Re: [Rails-core] Activerecord: how to write a new feature?

2012-08-09 Thread Rafael Almeida
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

Re: [Rails-core] Activerecord: how to write a new feature?

2012-08-09 Thread Rafael Almeida
Sorry for the double post, but I came up with arel: people.project(:id).join(services).on(services[:person_id].eq(people[:id])).where(services[:type].eq(1)).intersect( people.project(:id).join(services).on(services[:person_id].eq(people[:id])).where(services[:type].eq(2))) calling

[Rails-core] Activerecord: how to write a new feature?

2012-08-08 Thread Rafael Almeida
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

Re: [Rails-core] Activerecord: how to write a new feature?

2012-08-08 Thread Pedro Nascimento
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 almeida...@gmail.comwrote: Hello. Let's say I have the following schema People --- people_services --- services That is, people has a many-to-many