# Rails is doing what I want - but I don't understand how

**URL:** <https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500>\
**Category:** Rails\
**Created:** [August 18, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500 "2006-08-18T19:35:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [August 18, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500/1 "2006-08-18T19:35:02Z")

</div>

Hi guys, I have the strangest thing happening. The funny part is its  
doing exactly what I want to do, I just don’t understand how.

Basically here is my model.

class Role \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
has\_and\_belongs\_to\_many :rights

def self.names  
names = Array.new()  
for role in Role.find :all  
names \<\< role.name  
end  
return names  
end  
end

migration file for reference:

class CreateRoles \< ActiveRecord::Migration

def self.up  
create\_table :roles\_users, :id =\> false do |t|  
t.column “role\_id”, :integer  
t.column “user\_id”, :integer  
end

```
create_table :roles do |t|
t.column "name", :string
end

```

end

def self.down  
drop\_table :roles\_users  
drop\_table :roles  
end  
end

As you can see I wrote a self.names methods so that I can easily call  
all the names out of the roles table like so:

> > role\_names = Role.names  
> > =\> [“trainers”,“admins”]

So that makes perfect sense.

So the weird but cool part, is that when I go to grab all the names of  
the roles that belong to a user like so:

> > user = User.find :first  
> > =\> #\<User:0x25cd680 @attributes={“id”=\>“1”,  
> > “login”=\>“trainer”,“email”=\>“removed\_email\_address@domain.invalid”}\>
> 
> > user.roles  
> > =\> [#\<Role:0x25c6998 @attributes={“name”=\>“trainer”, “role\_id”=\>“1”,  
> > “id”=\>“1”, “user\_id”=\>“3”}\>]
> 
> > user.roles.names  
> > =\> [“trainer”]

How does that work?  
I aspected it to error out, or even require some sort of special  
instance ‘names’ definition at the very least.

So, I’ve been digging and have released that ‘roles’ just calls a  
special has\_and\_belongs\_to\_many find definition, but I’m still floored  
that this works the why I want it to - but just can’t grasp what is  
going on. I’m guessing that the Role.find methods I’m calling from  
within the self.names method is using a cached copy of all the roles  
found earlier by the user.roles methods???

Any suggestions or riddle solving would be greatly appreciated.  
Thanks,  
Jim

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [August 18, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500/2 "2006-08-18T20:39:13Z")

</div>

you’re mostly right. here’s an example to see why it works

try doing

user.roles.ids

you should get a NoMethodError: undefined method `ids’ for Role:Class

Note the Role:Class…this is the key…see you DO have a names class  
method for Role

so yes, it is a bit of rails ‘magic’ going on. i’m not sure exactly  
how it works though as the query that is generated is NOT the find  
:all query

ans a matter of fact, both the below calls generates the same query.

user.roles  
user.roles.names

my guess is that there is some sort of with\_scope or association  
extension going on…as if you were to remove the names method from  
your Role class and add this association extension

has\_and\_belongs\_to\_many :roles, :join\_table =\> “roles\_users” do  
def names  
find(:all).collect { |p| p.name }  
end  
end

to your user class, you would see the same results only you don’t have  
the benefit of having a class method any more.

that is quite strange though. perhaps someone else can provide more  
details on this behavior.

Chris

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [August 18, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500/3 "2006-08-18T20:54:14Z")

</div>

On Aug 18, 2006, at 10:35 AM, Jim F. wrote:

> ```
> names = Array.new()
> for role in Role.find :all
> names << role.name
> end
> return names
> 
> ```
> 
> end  
> end

I’m not sure about the rest of the email, but your names method could  
be simpler:

def self.names  
find(:all).collect { |r| r.name }  
end

🙂

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [August 18, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500/4 "2006-08-18T20:57:14Z")

</div>

Chris H. wrote:

> so yes, it is a bit of rails ‘magic’ going on. i’m not sure exactly  
> how it works though as the query that is generated is NOT the find  
> :all query
> 
> ans a matter of fact, both the below calls generates the same query.
> 
> user.roles  
> user.roles.names

Given that you can do user.roles.find(…), and this adds the condition  
that the role found belongs to the user (and the find appears as a class  
method on Role), it looks as if the same magic is being applied to the  
database call in the names class method.

You can check the query by looking in the development.log.

regards

Justin

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [August 18, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500/5 "2006-08-18T21:09:16Z")

</div>

On 8/18/06, Jim F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
…

> =\> [“trainer”]  
> found earlier by the user.roles methods???
> 
> Any suggestions or riddle solving would be greatly appreciated.  
> Thanks,  
> Jim

Hi Jim

It’s not that complicated: first, there is a proxy class that is being  
used here called HasAndBelongsToManyAssociation. It captures the  
basic method calls/messages sent to the associated collection, so  
user.roles for example. In HasAndBelongsToManyAssociation (which is  
by the way defined in  
active\_record/associations/has\_and\_belongs\_to\_many\_association.rb)  
there is a method\_missing. Take a look at its definition to  
understand.

Cheers,

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [August 18, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500/6 "2006-08-18T21:06:16Z")

</div>

Not sure I’m seeing straight or completely understanding the question  
but it seems to me that that will only work when the given user only  
has a single role, otherwise you’ll have to iterate over each roll to  
get the names? Am I missing the point here? I don’t see the confusion.

Tim

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [August 18, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500/7 "2006-08-18T21:25:07Z")

</div>

Bosko M. wrote:

> On 8/18/06, Jim F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> …
> 
> > =\> [“trainer”]  
> > found earlier by the user.roles methods???
> > 
> > Any suggestions or riddle solving would be greatly appreciated.  
> > Thanks,  
> > Jim
> 
> Hi Jim
> 
> It’s not that complicated: first, there is a proxy class that is being  
> used here called HasAndBelongsToManyAssociation. It captures the  
> basic method calls/messages sent to the associated collection, so  
> user.roles for example. In HasAndBelongsToManyAssociation (which is  
> by the way defined in  
> active\_record/associations/has\_and\_belongs\_to\_many\_association.rb)  
> there is a method\_missing. Take a look at its definition to  
> understand.
> 
> Cheers,

def method\_missing(method, \*args, &block)  
if @target.respond\_to?(method) ||  
(!@reflection.klass.respond\_to?(method) && Class.respond\_to?(method))  
super  
else  
@reflection.klass.with\_scope(:find =\> { :conditions =\>  
@finder\_sql, :joins =\> @join\_sql, :readonly =\> false }) do  
@reflection.klass.send(method, \*args, &block)  
end  
end  
end

I’m guessing it’s the:  
@reflection.klass.with\_scope(:find =\> {:conditions =\> @find\_sql,…})

portion that is narrowing the scope of the find method in  
user.roles.find(:all)??

Also, I was aware of the proxy class prior to posting, I’m just not far  
enough along in my railing to dig through all that code to find where  
the magic was happening, and for that I appologize.

Now with that said, did I just discover a safe way to implement future  
collect type definitions? My primary concern was that the “magic” was  
unintentional. As long as I know that this way is the best way to get  
what I want then I’ll be happy.

Thanks guys,  
Jim

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [August 18, 2006, 10:24pm UTC](https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500/8 "2006-08-18T22:24:25Z")

</div>

Tim McIntyre wrote:

> Not sure I’m seeing straight or completely understanding the question  
> but it seems to me that that will only work when the given user only has  
> a single role, otherwise you’ll have to iterate over each roll to get  
> the names?

No, this is about how the names method that the OP defined on Role is  
getting magically called via the roles collection on User,  
simultaneously passing in an extra condition. See Bosko’s contributions  
to this thread.

regards

Justin

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [August 18, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500/9 "2006-08-18T21:57:23Z")

</div>

On 8/18/06, Jim F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Hi Jim  
> > Cheers,  
> > end  
> > end  
> > end
> 
> I’m guessing it’s the:  
> @reflection.klass.with\_scope(:find =\> {:conditions =\> @find\_sql,…})
> 
> portion that is narrowing the scope of the find method in  
> user.roles.find(:all)??

Not in that call, no. The above gets caught by the proxy class’ own  
definition of find. It doesn’t hit the method\_missing code.

> Also, I was aware of the proxy class prior to posting, I’m just not far  
> enough along in my railing to dig through all that code to find where  
> the magic was happening, and for that I appologize.

Absolutely no need to appologize! Particularly not to me. 🙂

> Now with that said, did I just discover a safe way to implement future  
> collect type definitions? My primary concern was that the “magic” was  
> unintentional. As long as I know that this way is the best way to get  
> what I want then I’ll be happy.
> 
> Thanks guys,  
> Jim

You can in fact implement things that way, but I don’t recommend it.  
I am not convinced that it was intentionally designed to be used for  
YOUR class methods, or if you should rely on this behavior for a long  
time (you probably could, but I don’t). For the sake of clarity, I  
tend to prefer just calling ClassName.my\_class\_method(my\_parameters)  
from my own code, because then the scope of all the finds inside the  
class method is obvious just by looking at the class method call. But  
certainly, in the example you cited, the find(:all) inside your  
“names” class method gets a narrower scope tossed to it by the  
method\_missing/proxy class code above.

Cheers,

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [August 18, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/rails-is-doing-what-i-want-but-i-dont-understand-how/65500/10 "2006-08-18T22:49:47Z")

</div>

Bosko M. wrote:

> On 8/18/06, Jim F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > Hi Jim  
> > > Cheers,  
> > > end  
> > > end  
> > > end
> > 
> > I’m guessing it’s the:  
> > @reflection.klass.with\_scope(:find =\> {:conditions =\> @find\_sql,…})
> > 
> > portion that is narrowing the scope of the find method in  
> > user.roles.find(:all)??
> 
> Not in that call, no. The above gets caught by the proxy class’ own  
> definition of find. It doesn’t hit the method\_missing code.
> 
> > Also, I was aware of the proxy class prior to posting, I’m just not far  
> > enough along in my railing to dig through all that code to find where  
> > the magic was happening, and for that I appologize.
> 
> Absolutely no need to appologize! Particularly not to me. 🙂
> 
> > Now with that said, did I just discover a safe way to implement future  
> > collect type definitions? My primary concern was that the “magic” was  
> > unintentional. As long as I know that this way is the best way to get  
> > what I want then I’ll be happy.
> > 
> > Thanks guys,  
> > Jim
> 
> You can in fact implement things that way, but I don’t recommend it.  
> I am not convinced that it was intentionally designed to be used for  
> YOUR class methods, or if you should rely on this behavior for a long  
> time (you probably could, but I don’t). For the sake of clarity, I  
> tend to prefer just calling ClassName.my\_class\_method(my\_parameters)  
> from my own code, because then the scope of all the finds inside the  
> class method is obvious just by looking at the class method call. But  
> certainly, in the example you cited, the find(:all) inside your  
> “names” class method gets a narrower scope tossed to it by the  
> method\_missing/proxy class code above.
> 
> Cheers,

Thank,

I’m going to put this down as “undiscovered/unintended feature” and  
steer clear until someone at the top confirms that it will stick around  
permanently.

Thanks everyone,  
Jim
