Associations help?

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
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

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:incompute_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

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
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

Nope - ignore me and my stupid suggestion.

:wink:

Craig

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:incompute_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:inconst_missing’
from (eval):1:in compute_type' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/reflection.rb:112:inklass’
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:inreplace’
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:inattributes=’
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:ininitialize_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:increate’
from (irb):5
from :0

anyone have any ideas?

-Mike

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