On 5 January 2011 17:38, Fernando Leandro <[email protected]> wrote:
> Hello guys,
>
> I have a problem with how to do a sql query.
>
> I have 3 tables: courses, times and courses_times
>
> So for example, if i have the following data:
>
>  Courses                   courses_times                   Times
> id  |   name               course_id | time_id            id   |  day
>
> 1   | Math                            1  |  1                     1   |
> Monday
> 2   | Chimestry                     1  |  2                     2   |
> Wednesday
> 3   | History                         1  |  3                     3   |
> Friday
> 4   | Geografy                      2  |  1
>                                           2  |  2
>                                           3  |  1
>
>
> So, now, i want to make a query that returns to me only the courses that are
> ministred on Monday,
> but just ONLY Monday. For example, if a course is ministred Monday and
> Wednesday i dont want.
> In this example, i would like that my query returns just History, because is
> the only course
> that is just on Monday.
>
>
> SELECT courses.*
> FROM courses
> INNER JOIN courses_times ON courses_times.course_id = courses.id
> INNER JOIN times ON courses_times.time_id = times.id
> AND times.day = 'Monday'
>
> 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..

You can make it a Rails question by re-formulating the question in
terms of your ActiveRecord relationships and asking how to code a find
operation to return the objects you want.  In practice this should be
your initial approach anyway, only resorting to SQL if you cannot code
it using Rails helpers.

Colin

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to