嵌套有许多通过插件和命名范围
我有一个用户模型(:名称,:密码,:电子邮件),事件模型(:名称,:等)和兴趣模型(:名称)[>所有单数<]
然后我创建了两个连接表 -&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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你一定是一路上命名错了。乍一看,我会说你有一个文件或类命名不正确。请记住,模型名称必须始终是单数,无论是在文件名还是类名中,否则 Rails 将无法建立连接。问题的另一个根源是belongs_to 的参数也必须是单数。即使您做对了,当您运行命名范围时,HABTM 与用户的利益关系也会引发错误。
我能够使用以下模型解决您的错误。
user.rb
users_interest.rb
interest.rb
**events_interest.rb
event.rb
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
users_interest.rb
interest.rb
**events_interest.rb
event.rb