# Associations help?

**URL:** <https://www.ruby-forum.com/t/associations-help/77321>\
**Category:** Rails\
**Created:** [November 26, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/associations-help/77321 "2006-11-26T23:17:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [November 26, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/associations-help/77321/1 "2006-11-26T23:17:52Z")

</div>

I have Users who can “own” Projects. There is only one owner. Users in  
general can be authorized to view certain projects. Here is my initial  
stab  
at this:

class User \< ActiveRecord::Base  
has\_many :projects # ownership  
has\_many :project\_viewers, :dependent =\> :destroy  
has\_many :projects, :through =\> :project\_viewers, :uniq =\> true  
…  
end

class ProjectViewers \< ActiveRecord::Base  
belongs\_to :projects  
belongs\_to :users  
end

class Project \< ActiveRecord::Base  
belongs\_to :owner, :class\_name =\> ‘User’, :foreign\_key =\>  
‘owner\_id’  
has\_many :project\_viewers, :dependent =\> :destroy  
has\_many :users, :through =\> :project\_viewers, :uniq =\> true  
…  
end

Things appear to work ok until I go to destroy a User, at which point I  
get:

…/activesupport/lib/active\_support/dependencies.rb:399:in  
`to_constant_name': Anonymous modules have no name to be referenced by (eval):1:in `compute\_type’

Any help appreciated this fine Sunday!

Thanks,

## s.ross

View this message in context:  
[http://www.nabble.com/associations-help--tf2708701.html#a7551954](http://www.nabble.com/associations-help--tf2708701.html#a7551954)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [November 26, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/associations-help/77321/2 "2006-11-26T23:27:59Z")

</div>

On Sun, 2006-11-26 at 14:15 -0800, s.ross wrote:

> end  
> …  
> end
> 
> Things appear to work ok until I go to destroy a User, at which point I get:
> 
> …/activesupport/lib/active\_support/dependencies.rb:399:in  
> `to_constant_name': Anonymous modules have no name to be referenced by (eval):1:in`compute\_type’
> 
> Any help appreciated this fine Sunday!

* * *

Can you have 2 relationships to the same table? Wouldn’t you be better  
served by having the :dependent =\> :destroy attached to projects  
instead?

Craig

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [November 26, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/associations-help/77321/3 "2006-11-26T23:46:02Z")

</div>

I’m not trying to destroy the Project. I’m trying to destroy the record  
in  
the join table. Am I missing something?

Craig W. wrote:

> > has\_many :projects # ownership  
> > class Project \< ActiveRecord::Base  
> > `to\_constant\_name’: Anonymous modules have no name to be referenced by
> 
> >

–  
View this message in context:  
[http://www.nabble.com/associations-help--tf2708701.html#a7552202](http://www.nabble.com/associations-help--tf2708701.html#a7552202)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [November 26, 2006, 11:56pm UTC](https://www.ruby-forum.com/t/associations-help/77321/4 "2006-11-26T23:56:04Z")

</div>

Nope - ignore me and my stupid suggestion.

😉

Craig

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [December 6, 2006, 7:29pm UTC](https://www.ruby-forum.com/t/associations-help/77321/5 "2006-12-06T19:29:42Z")

</div>

I’m having a similar problem, but I get the error when I try to create a  
new object:

class Transfer \< ActiveRecord::Base

belongs\_to :account\_id  
validates\_presence\_of :account\_id

…

end

class AccountTransfer \< Transfer

validates\_presence\_of :matching\_transaction\_id

end

class Withdrawal \< Transfer

validates\_presence\_of :destination\_text

end

class Deposit \< Transfer

validates\_presence\_of :source\_text

end

when I try to create a new object, I get the following error:

> > Deposit.create(:account\_id=\>1)  
> > ArgumentError:  
> > ./script/…/config/…/config/…/vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/dependencies.rb:399:in  
> > `to_constant_name': Anonymous modules have no name to be referenced by from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/base.rb:1363:in`compute\_type’  
> > from  
> > ./script/…/config/…/config/…/vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/dependencies.rb:211:in  
> > `qualified_name_for' from ./script/../config/../config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:470:in`const\_missing’  
> > from (eval):1:in `compute_type' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/reflection.rb:112:in`klass’  
> > from  
> > ./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/associations/association\_proxy.rb:147:in  
> > `raise_on_type_mismatch' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb:22:in`replace’  
> > from  
> > ./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/associations.rb:894:in  
> > `account_id=' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/base.rb:1660:in`attributes=’  
> > from  
> > ./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/base.rb:1659:in  
> > `attributes=' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/base.rb:1493:in`initialize\_without\_callbacks’  
> > from  
> > ./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/callbacks.rb:225:in  
> > `initialize' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/base.rb:446:in`create’  
> > from (irb):5  
> > from :0

anyone have any ideas?

-Mike

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [December 7, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/associations-help/77321/6 "2006-12-07T21:42:45Z")

</div>

I figured out the problem – I stupidly had written “belongs\_to  
:account\_id” instead of just “belongs\_to :account”.

Hope this helps someone else save a headache!

-Mike
