# Distinct?

**URL:** <https://www.ruby-forum.com/t/distinct/114843>\
**Category:** Rails\
**Created:** [September 18, 2007, 1:38am UTC](https://www.ruby-forum.com/t/distinct/114843 "2007-09-18T01:38:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 18, 2007, 1:38am UTC](https://www.ruby-forum.com/t/distinct/114843/1 "2007-09-18T01:38:26Z")

</div>

This should be easy, but I’m stumped…

class X \< ActiveRecord::Base  
belongs\_to :y  
end  
class Y \< ActiveRecord::Base  
has\_many :x  
end

The y table has a column names. The x table has a column value. I  
want the list of values in x for the name “Location” in y. Yes, there  
are many repetitions.

I can probably hack together an SQL statement to do this without too  
much trouble, but the point of using rails is kinda to avoid that when  
we can, right?

Student

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 18, 2007, 2:16am UTC](https://www.ruby-forum.com/t/distinct/114843/2 "2007-09-18T02:16:28Z")

</div>

Hi, if you would like to obtain the x collection of y, you simply need  
to do  
the following:

renaming y to teacher

renaming x to student

Now, we can say the following:

a\_teacher.students

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 18, 2007, 5:14am UTC](https://www.ruby-forum.com/t/distinct/114843/3 "2007-09-18T05:14:46Z")

</div>

No, I’m looking for the names of the teachers who teach fifth grade.

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 18, 2007, 5:23am UTC](https://www.ruby-forum.com/t/distinct/114843/4 "2007-09-18T05:23:46Z")

</div>

No, I’m looking for the names of all of the teachers who are teaching  
students that are in the fifth grade. (The students are in the fifth  
grade.)

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 18, 2007, 2:39pm UTC](https://www.ruby-forum.com/t/distinct/114843/5 "2007-09-18T14:39:00Z")

</div>

No, I’m looking for the first names of the students of the band  
teachers.

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 18, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/distinct/114843/6 "2007-09-18T15:01:09Z")

</div>

On 9/17/07, Student [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The y table has a column names. The x table has a column value. I  
> want the list of values in x for the name “Location” in y. Yes, there  
> are many repetitions.

You first find the Y row for “Location”:

row = Y.find\_by\_names(‘Location’)

That returns nil if there is no such row. If there are multiple rows,  
it returns only the first. If you want all matches, you need to use

rows = Y.find\_all\_by\_names(‘Location’)

Then all the associated X rows can be found via the “x” method of the Y  
object:

results = row.x.collect :&value

(n.b. the normal convention is to use the plural form in has\_many;  
i.e. “has\_many :xs”)

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 18, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/distinct/114843/7 "2007-09-18T15:07:09Z")

</div>

On 9/18/07, Student [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> No, I’m looking for the first names of the students of the band  
> teachers.

That doesn’t match your original description of the problem.

Why not give us the actual models, columns, etc. It’s hard to think in  
terms of x’s and y’s.

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 18, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/distinct/114843/8 "2007-09-18T15:19:11Z")

</div>

So I see.

class Subprojectattr \< ActiveRecord::Base  
belongs\_to :attribute  
end  
class Attribute \< ActiveRecord::Base  
has\_many :subprojectattrs  
end

So I want the list of Subprojectattr.valueStrs when spa.attribute.name  
== “Location”. Yes, this is a legacy database, so I cannot fix the  
organizational problems via schema changes.

Student

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 18, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/distinct/114843/9 "2007-09-18T15:33:17Z")

</div>

BTW, I get the results I want with the following sql:

SELECT dbo.subProjectAttr.valueStr  
FROM dbo.subProjectAttr INNER JOIN  
dbo.attribute ON dbo.subProjectAttr.attributeId  
= dbo.attribute.attributeId  
WHERE (dbo.attribute.name = ‘Location’)  
GROUP BY dbo.subProjectAttr.valueStr

As I said, however, the idea is to avoid having to put this stuff in  
directly.
