Ruby on Rails Wednesday, January 5, 2011

On Jan 5, 5:38 pm, Fernando Leandro <fernandoleandro1...@gmail.com>
wrote:

> When i use a query like above, for example, it returns to me all the courses
> that have association with Monday,
> it returns Math, Chimestry and History, and i would like to receive just
> History
>
> I know that this question is not about rails but i need to solve this
> problem to continue my rails app,
> and i would appreciate if someone could help me..
>
> Does anyone have a solution for this?

In sql terms, how about

select *, count(*) as times_scheduled from course_times
inner join course_times as other_times on other_times.course_id =
course_times.course_id
where course_times.time_id = 1
group by course_times.course_id
having times_scheduled = 1

Arel is supposed to be way better than old skool activerecord at
generating joins like this where you need to be careful about aliasing
table names and so on.

Fred

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment