嵌套有许多通过插件和命名范围

发布于 2024-08-19 13:47:50 字数 1496 浏览 1 评论 0原文

我有一个用户模型(:名称,:密码,:电子邮件),事件模型(:名称,:等)和兴趣模型(:名称)[>所有单数<]

然后我创建了两个连接表 -&gt;用户兴趣和事件兴趣;每个不包含主键,仅分别由 user_id/interest_id 和 event_id/interest_id 组成。 [>plural<]

我的模型使用 Nested Has Many Through 插件

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

唷..ok 所以我想要创建一个命名范围,查找与特定用户共享兴趣的所有事件。这是有人帮助我的一些代码。

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

当我从控制器运行时 =>

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

我收到错误:

uninitialized constant Event::EventsInterest

任何人都可以看到我搞砸了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨洒年华 2024-08-26 13:47:50

你一定是一路上命名错了。乍一看,我会说你有一个文件或类命名不正确。请记住,模型名称必须始终是单数,无论是在文件名还是类名中,否则 Rails 将无法建立连接。问题的另一个根源是belongs_to 的参数也必须是单数。即使您做对了,当您运行命名范围时,HABTM 与用户的利益关系也会引发错误。

我能够使用以下模型解决您的错误。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文