2

我正在尝试连接我拥有的两个连接表的值并根据条件关系显示结果......我遇到了一些问题

我有一个用户模型(:名称,:密码,:电子邮件),事件模型(:名称,:etc)和兴趣模型(:名称)

我在每个模型中创建了大约 5 条记录。

然后我创建了两个连接表 -> UsersInterests 和 EventsInterests;每个不包含主键,仅分别由 user_id/interest_id 和 event_id/interest_id 组成。

然后我在模型文件中添加了 HABTM 关系

users => has_and_belongs_to_many :interests
events => has_and_belongs_to_many :interests
interests => has_and_belongs_to_many :users
         has_and_belongs_to_many :events

现在我想创建一个控制器,它只查找用户兴趣与事件兴趣相对应的事件

经过一段时间的研究,我发现我需要在

@Events = Event.User.find([condition])
[condition] = where users.interest == event.interest

或类似的东西......我有点迷路了......你如何陈述查找条件?......我知道如何在sql中进行内部连接,但我正在寻找优雅的Rails方式来做到这一点。 ..有什么建议吗?

4

1 回答 1

7

优雅的 ruby​​ 方法是使用命名范围。但是,因为您决定使用 has_and_belongs_to_many 关系而不是 has_many :through 关系,所以您将需要使用原始 SQL 定义连接,这不是很优雅。而且由于 Rails 处理 SQL 生成的方式,您必须创建一个供单个用户使用的范围,以及另一个用于多个用户的命名范围。

Class Event < ActiveRecord::Base
  ...

  #find events that share an interest with a single user
  named_scope :shares_interest_with_user, lambda {|user|
    { :joins => "LEFT JOIN events_interests ei ON ei.event_id = events.id " +
         "LEFT JOIN users_intersets ui ON ui.interest_id = ei.interest_id",
      :conditions => ["ui.user_id = ?", user], :group_by => "events.id"
    }

  #find events that share an interest with a list of users
  named_scope :shares_interest_with_users, lambda {|users|
    { :joins => "LEFT JOIN events_interests ei ON ei.event_id = events.id " +
         "LEFT JOIN users_intersets ui ON ui.interest_id = ei.interest_id",
      :conditions => ["ui.user_id IN ?", users], :group_by => "events.id"
    }    
  }

  #find events that share an interest with any user
  named_scope :shares_interest_with_any_user, lambda {
    { :joins => "LEFT JOIN events_interests ei ON ei.event_id = events.id " +
         "JOIN users_intersets ui ON ui.interest_id = ei.interest_id",
      :conditions => "ui.user_id IS NOT NULL", :group_by => "events.id"
    }    
  }

end

现在您可以这样做来获取用户可能感兴趣的所有事件:

@events = Event.shares_interest_with_user(@user)

或者这是获取用户列表可能感兴趣的所有事件:

@events = Event.shares_interest_with_users(@users)

但正如我警告的那样,这并不是很优雅

如果通过正确的连接模型而不是 HABTM 关系将关系重新定义为 has_many,则可以大大简化连接。你的情况需要嵌套的有很多通过插件才能工作。注意您必须在所有其他模型中添加相应的 has_many/belongs_to 语句。甚至连接模型。

Class Event < ActiveRecord::Base
  has_many :event_interests
  has_many :interests, :through => :event_interests
  has_many :user_interests, :through => :interests
  has_many :users, :through => :user_interests
  ...   

  #find events that share an interest with a list of users
  named_scope :shares_interest_with_users, lambda {|user|
    { :joins => :user_interests, :group_by => "events.id",
      :conditions => {:user_interests => {:user_id => user}}
    }    
  }

  #find events that share an interest with any user
  named_scope :shares_interest_with_any_user, lambda {
    { :joins => :user_interests, :group_by => "events.id",
      :conditions => "user_interests.user_id IS NOT NULL"
    }

end

现在,以下将起作用。

@user = User.first; @users = User.find(1,2,3)

# @events = all events a single user would be interested in
@events = Event.shares_interest_with_users(@user)

# @events = all events any of the listed users would be interested in.
@events = Event.shares_interest_with_users(@user)

您甚至可以定义一个命名范围来选择尚未发生的事件并将两者链接起来:

named_scope :future_events, lambda {
   { :conditions => ["start_time > ?", Time.now]}
}

Events.future_events #=> Events that haven't started yet.

# Events that a user would be interested in but only choose those 
# that haven't started yet.
Events.future_events.shares_interest_with_user(@user) 
于 2010-02-01T04:45:30.677 回答