0

我正在使用sequelRuby 中的 gem 连接到 sqlite 数据库(由 rails 提供)。我有各种Users 和各种Projects。我想找到唯一的项目对象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?
4

1 回答 1

3

假设您有project_name并且user_name想要加入并找到与两者都匹配的项目:

Project.join(:users, :id=>:user_id).
 select(:projects.*).
 first(:users__name=>user_name, :projects__name=>project_name)
于 2011-08-23T16:48:38.393 回答