# Can I do a "named\_scope" for this scenario?

**URL:** <https://www.ruby-forum.com/t/can-i-do-a-named-scope-for-this-scenario/165388>\
**Category:** Rails\
**Created:** [April 13, 2009, 3:46pm UTC](https://www.ruby-forum.com/t/can-i-do-a-named-scope-for-this-scenario/165388 "2009-04-13T15:46:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [April 13, 2009, 3:46pm UTC](https://www.ruby-forum.com/t/can-i-do-a-named-scope-for-this-scenario/165388/1 "2009-04-13T15:46:36Z")

</div>

Hi,  
Is it possible to do a named\_scope (or “has\_many, :through…”) for this  
scenario:

- Class: Node
- Class: Relationships

- node\_id
- dependant\_node\_id

QUESTION: So how can I get a “has\_many” or “named\_scope” happening  
within  
the Node class such that it would:

(a) pass back ALL relationships, including both those relationships for  
which the node instance is either referenced by (i) node\_id, or (ii)  
dependant\_node\_id. This might be called something like  
“all\_relationships”

(b) pass back ALL nodes at the other end of the relationships identified  
by  
(a), but not including in the returned array of nodes the instance node  
in  
question. This might be called something “all\_direct\_related\_nodes”.

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [April 14, 2009, 12:27am UTC](https://www.ruby-forum.com/t/can-i-do-a-named-scope-for-this-scenario/165388/2 "2009-04-14T00:27:07Z")

</div>

PS. To further clarify my question note that:

* * *

class Node \< ActiveRecord::Base  
has\_many :relationships\_as\_child, :class\_name =\> ‘Relationship’,  
:foreign\_key =\> :dependant\_node\_id  
has\_many :relationships\_as\_parent, :class\_name =\> ‘Relationship’,  
:foreign\_key =\> :node\_id

## def all\_relations Relationship.find(:all, :conditions =\> [“node\_id = ? OR dependant\_node\_id = ?”, self.id, self.id]) end

I can get the first two “has\_many” working, but I can’t work out how to  
effectively combine these two into one “has\_many”. I can create a "  
all\_relations" method however that does provide the full flexibility of  
a "  
has\_many" or a “named\_scope” version (which I’m after) as I can’t then  
do  
further things like:

Node.find(1).all\_relations.find(1) # fails

So in other words I want the output of the “all\_relations” function, but  
in  
the form of a “has\_many” or a “named\_scope”.

Any help welcomed 🙂

2009/4/13 Greg H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> the Node class such that it would:
> 
> –  
> Greg  
> [http://blog.gregnet.org/](http://blog.gregnet.org/)

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [April 15, 2009, 2:58am UTC](https://www.ruby-forum.com/t/can-i-do-a-named-scope-for-this-scenario/165388/3 "2009-04-15T02:58:27Z")

</div>

You could try:

named\_scope :all\_relations\_for, lambda { |o| { :conditions =\>  
[“node\_id = :o\_id OR dependant\_node\_id = :o\_id”, { :o\_id =\>  
o.id }] } }

That will let you do:

Node.all\_relations\_for(Node.find(1)) etc.

Not quite as snazzy as what you’re looking for, but close…

–Matt J.

On Apr 13, 6:26 pm, Greg H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [April 21, 2009, 4:15am UTC](https://www.ruby-forum.com/t/can-i-do-a-named-scope-for-this-scenario/165388/4 "2009-04-21T04:15:16Z")

</div>

thanks, but this doesn’t bring back Relationships, it brings back  
Nodes…does a named\_scope always bring back instances of the class it’s  
specified in it does it?

I really want a “named\_scope”, or “has\_many” version of the method  
“all\_relations” in my original email below…any more ideas anyone?

thanks

2009/4/15 Matt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > has\_many :relationships\_as\_child, :class\_name =\> ‘Relationship’,  
> > I can get the first two “has\_many” working, but I can’t work out how to  
> > the form of a “has\_many” or a “named\_scope”.
> > 
> > Any help welcomed 🙂
> > 
> > 2009/4/13 Greg H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> >

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [April 22, 2009, 3:33am UTC](https://www.ruby-forum.com/t/can-i-do-a-named-scope-for-this-scenario/165388/5 "2009-04-22T03:33:49Z")

</div>

I think my solution was aiming more for the all\_direct\_related\_models  
part; sorry for the confusion.

You should be able to do the other half with a named scope on  
Relationship, basically with the same scope:

class Relationship \< …  
named\_scope :all\_relations\_for, lambda { |o| { :conditions =\>  
[“node\_id = :o\_id OR dependant\_node\_id = :o\_id”, { :o\_id =\>  
o.id }] } }  
end

Then drop a method into your Node model:

class Node \< …  
def all\_relations  
Relationship.all\_relations\_for(self)  
end  
end

–Matt J.

On Apr 20, 10:14 pm, Greg H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
