2011-08-23 53 views
0

我使用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? 

回答

3

假設你有project_nameuser_name,並希望做一個連接,發現兩者相匹配的項目:

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