# A possible bug in Has\_many :through using :soucre

**URL:** <https://www.ruby-forum.com/t/a-possible-bug-in-has-many-through-using-soucre/84898>\
**Category:** Rails\
**Created:** [February 8, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/a-possible-bug-in-has-many-through-using-soucre/84898 "2007-02-08T14:25:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [February 8, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/a-possible-bug-in-has-many-through-using-soucre/84898/1 "2007-02-08T14:25:17Z")

</div>

I’ve been stumped on this for a few days and I’m not sure if it’s an  
error  
on my end or an actual bug.  
I have a system where I have workshops and users. Workshops have  
students,  
presenters, and troubleshooters. I wanted to represent this simply by  
using has\_many :through with :source.

Workshop

has\_many :presenters, :through =\>:presenterships, :source=\>:user

This actually works great. However, I am running into a really  
interesting  
problem when doing the assignments:

This works:

@u = User.find 1  
@w = Workshop.find 1  
@w.presenterships.create :user\_id =\> u.id

But this fails:

@u = User.find 1  
@w = Workshop.find 1  
@w.presenterships.create :user =\> u

Notice t he assignment of the object instead of the id field.

The trickiest part of all this is that I get a really strange error  
message:

ActiveRecord::AssociationTypeMismatch: User expected, got User  
from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2  
/lib/active\_recor  
d/associations/association\_proxy.rb:148:in `raise_on_type_mismatch' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2 /lib/active_recor d/associations/belongs_to_association.rb:22:in`replace’  
from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2  
/lib/active\_recor  
d/associations.rb:900:in `user=' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2 /lib/active_recor d/base.rb:1673:in`attributes=’  
from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2  
/lib/active\_recor  
d/base.rb:1672:in `attributes=' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2 /lib/active_recor d/base.rb:1506:in`initialize\_without\_callbacks’  
from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2  
/lib/active\_recor  
d/callbacks.rb:225:in `initialize' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2 /lib/active_recor d/associations/has_many_association.rb:13:in`build’  
from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2  
/lib/active\_recor  
d/associations/association\_collection.rb:93:in `create’  
from (irb):5

I’ve pasted my classes below. The migrations are standard  
(presenterships  
just has its own id and the two foreign keys). I’d appreciate any  
insight.  
I have a workaround but I was just wondering if this was a bug or a  
limitation.

class Workshop \< ActiveRecord::Base

has\_many :presenterships  
has\_many :presenters, :through =\>:presenterships, :source=\>:user

end

class Presentership \< ActiveRecord::Base  
belongs\_to :workshop  
belongs\_to :user  
end

class User

# lots of validations

end

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [February 8, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/a-possible-bug-in-has-many-through-using-soucre/84898/2 "2007-02-08T15:11:22Z")

</div>

i tested your example exactly as you posted it and all works fine for  
me using Rails 1.1.6.

> > w = Workshop.find 1  
> > =\> #\<Workshop:0xb797928c @attributes={“name”=\>“RailsConf”, “id”=\>“1”}\>

> > users = User.find(:all)  
> > =\> [#\<User:0xb792f650 @attributes={“name”=\>“Bob”, “id”=\>“1”}\>,  
> > #\<User:0xb792f614 @attributes={“name”=\>“Joe”, “id”=\>“2”}\>]

> > w.presenterships.create :user\_id =\> users[0].id  
> > =\> #\<Presentership:0xb7923828  
> > @errors=#\<ActiveRecord::Errors:0xb7922d60 @errors={},  
> > @base=#\<Presentership:0xb7923828 …\>\>, @attributes={“workshop\_id”=\>1,  
> > “id”=\>1, “user\_id”=\>1}, @new\_record=false\>

> > w.presenterships.create :user =\> users[1]  
> > =\> #\<Presentership:0xb791cd5c  
> > @errors=#\<ActiveRecord::Errors:0xb791c438 @errors={},  
> > @base=#\<Presentership:0xb791cd5c …\>\>, @attributes={“workshop\_id”=\>1,  
> > “id”=\>2, “user\_id”=\>2}, @user=#\<User:0xb792f614  
> > @attributes={“name”=\>“Joe”, “id”=\>“2”}\>, @new\_record=false\>

> > w.presenters true  
> > =\> [#\<User:0xb7913be4 @attributes={“name”=\>“Bob”, “id”=\>“1”}\>,  
> > #\<User:0xb79139c8 @attributes={“name”=\>“Joe”, “id”=\>“2”}\>]

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [February 8, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/a-possible-bug-in-has-many-through-using-soucre/84898/3 "2007-02-08T15:48:26Z")

</div>

Yeah… forgot to mention that I am using 1.2.1 (and now 1.2.2)  
Could you confirm this?

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [February 8, 2007, 7:42pm UTC](https://www.ruby-forum.com/t/a-possible-bug-in-has-many-through-using-soucre/84898/4 "2007-02-08T19:42:53Z")

</div>

@Chris:  
Thanks for trying to duplicate. I’ve recreated this in a new project and  
it  
worked fine, so that means the current project is doing something  
something  
else that’s causing the incompatibility issue.

I was afraid it was something I did, and looks like I was right.

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [February 8, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/a-possible-bug-in-has-many-through-using-soucre/84898/5 "2007-02-08T17:38:42Z")

</div>

works for 1.2.2 for me as well

> > RAILS\_GEM\_VERSION  
> > =\> “1.2.2”
> 
> > w = Workshop.find :first  
> > =\> #\<Workshop:0xb7629168 @attributes={“name”=\>“RailsConf”, “id”=\>“1”}\>
> 
> > users = User.find(:all)  
> > =\> [#\<User:0xb7620964 @attributes={“name”=\>“Bob”, “id”=\>“1”}\>,  
> > #\<User:0xb7620928 @attributes={“name”=\>“Joe”, “id”=\>“2”}\>]
> 
> > w.presenterships.clear  
> > =\> []

> > w.presenterships.create :user\_id =\> users[0].id  
> > =\> #\<Presentership:0xb760c428  
> > @errors=#\<ActiveRecord::Errors:0xb760b898 @errors={},  
> > @base=#\<Presentership:0xb760c428 …\>\>, @attributes={“workshop\_id”=\>1,  
> > “id”=\>5, “user\_id”=\>1}, @new\_record=false\>

> > w.presenterships.create :user =\> users[1]  
> > =\> #\<Presentership:0xb76066e0  
> > @errors=#\<ActiveRecord::Errors:0xb7605da8 @errors={},  
> > @base=#\<Presentership:0xb76066e0 …\>\>, @attributes={“workshop\_id”=\>1,  
> > “id”=\>6, “user\_id”=\>2}, @new\_record=false, @user=#\<User:0xb7620928  
> > @attributes={“name”=\>“Joe”, “id”=\>“2”}\>\>

> > w.presenterships  
> > =\> [#\<Presentership:0xb760c428  
> > @errors=#\<ActiveRecord::Errors:0xb760b898 @errors={},  
> > @base=#\<Presentership:0xb760c428 …\>\>, @attributes={“workshop\_id”=\>1,  
> > “id”=\>5, “user\_id”=\>1}, @new\_record=false\>, #\<Presentership:0xb76066e0  
> > @errors=#\<ActiveRecord::Errors:0xb7605da8 @errors={},  
> > @base=#\<Presentership:0xb76066e0 …\>\>, @attributes={“workshop\_id”=\>1,  
> > “id”=\>6, “user\_id”=\>2}, @new\_record=false, @user=#\<User:0xb7620928  
> > @attributes={“name”=\>“Joe”, “id”=\>“2”}\>\>]

> > w.presenters true  
> > =\> [#\<User:0xb75ff8e0 @attributes={“name”=\>“Bob”, “id”=\>“1”}\>,  
> > #\<User:0xb75ff8b8 @attributes={“name”=\>“Joe”, “id”=\>“2”}\>]

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [April 28, 2007, 7:38am UTC](https://www.ruby-forum.com/t/a-possible-bug-in-has-many-through-using-soucre/84898/6 "2007-04-28T07:38:48Z")

</div>

On 2/8/07, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I was afraid it was something I did, and looks like I was right.

I don’t think it’s something you did. Check the thread titled  
“AssociationTypeMismatch:  
RoleType expected, got RoleType”  
I’m having the same problem and I haven’t changed anything…only  
upgraded  
Rails. I think the reason these guys weren’t able to repeat is that it  
always works the first time you restart your server, but after that it’s  
broken. Wondering if someone has filed this as an official bug…

Jake
