在续集中使用基本连接

时间:2011-08-23 00:27:06

标签: sql ruby sequel

我正在使用Ruby中的sequel gem连接到sqlite数据库(由rails提供)。我有各种User和各种Project s。我想找到唯一的项目对象user.username/project.name,如果它存在的话。这样做最优雅的方式是什么?我有连接工作等我有:

DB = Sequel.connect 'sqlite:///path/to/sqlite'
class User < Sequel::Model
end

class Project < Sequel::Model
end

# How do I retrieve the project object using project_name, user_name
#   project.name == project_name
#   project.user_id = xxx
#   and there is a user with id xxx and username user_name?

1 个答案:

答案 0 :(得分:3)

假设您拥有project_nameuser_name并希望进行加入并找到与两者兼容的项目:

Project.join(:users, :id=>:user_id).
 select(:projects.*).
 first(:users__name=>user_name, :projects__name=>project_name)