# Has\_and\_belongs\_to\_many and dynamic find

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-and-dynamic-find/107599>\
**Category:** Rails\
**Created:** [June 23, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-and-dynamic-find/107599 "2007-06-23T21:44:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9f8e36/32.png) [@qwan](https://www.ruby-forum.com/u/qwan)\
**Post date:** [June 23, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-and-dynamic-find/107599/1 "2007-06-23T21:44:41Z")

</div>

Hi

Just curious if anyone can explain why using a dynamic find fails to  
work with \<\< operator

I have standard habtm relationship

class User \< ActiveRecord::Base

has\_and\_belongs\_to\_many :roles

Now when I assign a Role via \<\< after saving the new User I get wierd  
behaviour  
but only when using the dynamic version of find

i.e

@user = User.new§

```
  if @user.save

    # FOLLOWING DOES NOT WORK
    # @user.roles << Role.find_by_name( 'registered' )

    # But this this is Fine !
    @user.roles << Role.find( :first, :conditions => ['name = ?',

```

‘registered’] )

When using the usal find( :first) style everythign is sweet,  
associaiton is built and appears in DB.

However when using the find\_by\_name association\_proxy.rb throws an  
exception but the error I get is somewhat cryptic as it appears as if  
the result of find\_by\_name is a ‘Role’ as expected :

“Role expected, got Role”

Chances are I’m doing something obviously wrong, and not really a  
problemn as I can use find :first … but still curious, why does  
find\_by\_name blow up ?

Cheers all,  
tom

The full trace is here

C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/  
associations/association\_proxy.rb:148:in `raise_on_type_mismatch' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ associations/association_collection.rb:24:in`\<\<’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/  
associations/association\_collection.rb:23:in `each' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ associations/association_collection.rb:23:in`\<\<’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/  
connection\_adapters/abstract/database\_statements.rb:59:in  
`transaction' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ transactions.rb:95:in`transaction’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/  
transactions.rb:121:in `transaction' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ associations/association_collection.rb:22:in`\<\<’  
#{RAILS\_ROOT}/app/controllers/user\_controller.rb:176:in `new’

---

<div class="post-metadata">

**Author:** ![qwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9f8e36/32.png) [@qwan](https://www.ruby-forum.com/u/qwan)\
**Post date:** [June 24, 2007, 11:45am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-and-dynamic-find/107599/2 "2007-06-24T11:45:46Z")

</div>

> ```
> # FOLLOWING DOES NOT WORK
> # @user.roles << Role.find_by_name( 'registered' )
> 
> ```

if Role.find\_by\_name(‘registered’) returns more than one record (which,  
by definition :first will avoid) you may have this problem…

what-say-u? if the :name isn’t unique, and u got a couple names with  
‘registered’ for the value … you should be getting TypeMismatch, as  
you’re trying to associate an array instead of a Role to a @user.

makes any sense?

---

<div class="post-metadata">

**Author:** ![qwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9f8e36/32.png) [@qwan](https://www.ruby-forum.com/u/qwan)\
**Post date:** [June 26, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-and-dynamic-find/107599/3 "2007-06-26T17:29:51Z")

</div>

Hi shai, thanks for the response.

Yep does make sense but in this case there is definitly only one such  
Role in the DB,  
and also strikes me as odd that the error message says 'Role expected,  
got Role"

Of course the message may simply be constructed wrongly, but I would  
expect  
'Role expected, got Array"

If I get time I will try and unravel the dynamic version, as I would  
expect under the hood, the two calls should be equivalent.

cheers  
tom

---

<div class="post-metadata">

**Author:** ![qwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9f8e36/32.png) [@qwan](https://www.ruby-forum.com/u/qwan)\
**Post date:** [June 26, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-and-dynamic-find/107599/4 "2007-06-26T21:48:28Z")

</div>

Well looks like this is common problem, probably a rails bug … not  
sure if it’s been raised as such but anyway, I found a post with a  
good workaround

see…

[http://www.ruby-forum.com/topic/101817](http://www.ruby-forum.com/topic/101817)

where you define the assocation directly require the other model  
file … e.g. in user.rb add

require File.dirname( **FILE** )+‘/role.rb’
