views:

137

answers:

1

I have a User Model(:name, :password, :email), and Event model(:name, :etc) and Interest model (:name) [>all singular<]

Then I created two join tables -> UsersInterests and EventsInterests; each not containing a primary key and only comprised of the user_id/interest_id and event_id/interest_id respectively. [>plural<]

My Models Use the Nested Has Many Through Plugin

user.rb => has_many :users_interests
 has_many :interests, :through => :users_interests
 has_many :events_interests, :through => :interests
 has_many :events, :through => :events_interests

event.rb => has_many :events_interests
  has_many :interests, :through => :events_interests
     has_many :users_interests, :through => :interests
  has_many :users, :through => :users_interests

interest.rb => has_and_belongs_to_many :users
               has_and_belongs_to_many :events

events_interests.rb => belongs_to :interests
                    belongs_to :events
users_interests.rb => belongs_to :users
                   belongs_to :interests

Whew..ok So I wanted to created a named_scope of that find all the events that share interest with a particular user. Here is some code someone helped me with.

named_scope :shares_interest_with_users, lambda {|user|
{ :joins => :users_interests,
  :conditions => {:users_interests => {:user_id => user}}
   }}

When i run from the controller =>

@user = User.find(1) 
@events = Event.shares_interest_with_user(@user)

I get the error :

uninitialized constant Event::EventsInterest

Can anyone see what i messed up?

+1  A: 

You must have named something wrong along the way. At a glance I'd say you have a file or class named incorrectly. Remember model names MUST always be singular, both in file and class names or else Rails won't make the connection. Another source of your problem is that arguments to belongs_to must also be singular. Even if you had got things right, the HABTM relationship in interests with users would have thrown an error when you ran the named scope.

I was able to solve your error with the following models.

user.rb

class User < ActiveRecord::Base
has_many :users_interests
  has_many :interests, :through => :users_interests
  has_many :events_interests, :through => :interests
  has_many :events, :through => :events_interests
end

users_interest.rb

class UsersInterest < ActiveRecord::Base
  belongs_to :user
  belongs_to :interest
end

interest.rb

class Interest < ActiveRecord::Base 
  has_many :users,:through => :users_interests
  has_many :users_interests
  has_many :events_interests
  has_many :events, :through => :events_interests
end

**events_interest.rb

class EventsInterest <ActiveRecord::Base
  belongs_to :interest
  belongs_to :event
end

event.rb

class Event <ActiveRecord::Base 
  has_many :events_interests
  has_many :interests, :through => :events_interests
  has_many :users_interests, :through => :interests
  has_many :users, :through => :users_interests


  named_scope :shares_interest_with_users, lambda {|user|
    { :joins => :users_interests,
      :conditions => {:users_interests => {:user_id => user}}
    }
  }

end
EmFi