Rails: selecting columns from multiple tables

I am new to Rails. I have three tables a, b,c

bhas 2 columns: b1andb2

chas 2 columns: c1andc2

aIt has three columns: a1, b1(foreign key) and the c1(external key)

I want to get pairs distinct (b2, c2)by specifying a value fora1

I tried something like

a.find(:all, :joins => [:b, :c], :select => "b2, c2", :conditions => {:a => {:a1 => Time.now.midnight. ... Time.now}}, :group => "b2, c2")

SQLThat it gives good results, and I can see the results. But I think, as I do a.find, I can not remove b2, c2from the result set.

How can I change this to get b2and c2?

+3
source share
2 answers

:select, :

foo = a.find(:all, 
       :joins => [:b, :c], 
       :select => "distinct b.b2 as b2_value, c.c2 as c2_value", 
       :conditions => {:a => {:a1 => Time.now.midnight. ... Time.now}}, 
       :group => "b.b2, c.c2") 

, . :

b2_value = foo.first["b2_value"]

, .

, , , - , , (, created_at id).

+4

a.find(:all, :include => [:b, :c], :select => "distinct b2, c2", :conditions ...)
0

All Articles