On Jun 12, 6:48 pm, Brian <[email protected]> wrote:
> I have a table with an integer column. I want to retrieve pairs of
> records where this integer sums to a total.  For example:
>
> 4|red
> 6|blue
> 2|yellow
>
> If I want pairs that add up to 8 I should get [[red,red],[blue,yellow],
> [yellow,blue]]. Writing the query myself I would just use a join with
> no ON clause (I think this is a cross join):
>
> SELECT a.color, b.color FROM mytable a INNER JOIN mytable b WHERE
> a.value + b.value = 8
>
> This works just fine from a sqlite console, but when I try to use
> find_by_sql I can't get all of the information.  It attempts to fit
> everything into an instance of the model class, which means that I
> only see b.color.
>
> What is the 'rails' way of doing what I am trying to accomplish? Is
> there a set of options I can use with find to recreate this query and
> get both colors out? I'm hoping for an array of 2-element arrays.

You'll need to alias one of them (ie b.color as something_else). If
what you get back doesn't really correspond to an instance of your
class then using connectiion.select_all might be a better fit

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 [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