Using basic joins in sequel
I'm using the sequel
gem in Ruby to connect to a sqlite database (provided by rails). I have various User
s and various Project
s. I want to find the unique project object user.username/project.name
, if it exists. What's the most elegant way of doing this? I have the connection working etc. I have:
DB = Sequel.connect 'sqlite:///path/to/sqlite'
class User < Sequel::Model
end
cla开发者_StackOverflow中文版ss 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?
Assuming you have the project_name
and user_name
and want to do a join and find the project that matches both:
Project.join(:users, :id=>:user_id).
select(:projects.*).
first(:users__name=>user_name, :projects__name=>project_name)
精彩评论