HABTM关系和连接表

时间:2010-02-01 02:48:42

标签: ruby-on-rails relational-database has-and-belongs-to-many

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

我有一个用户模型(:name,:password,:email)和Events模型(:name,:etc)和Interests模型(:name)

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

然后我创建了两个连接表 - >用户兴趣和活动兴趣;每个都不包含主键,并且分别只包含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方式这样做...任何提示家伙?

1 个答案:

答案 0 :(得分:7)

优雅的红宝石方式是使用named scopes。但是因为您决定使用has_and_belongs_to_many关系而不是has_many:通过关系,您将需要使用原始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,则可以极大地简化连接。您的案例需要nested has many through plugin才能生效。注:您必须在所有其他模型中添加相应的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)