开发者

Using basic joins in sequel

开发者 https://www.devze.com 2023-03-29 18:51 出处:网络
I\'m using the sequel gem in Ruby to connect to a sqlite database (provided by rails). I have various Users and various Projects. I want to find the unique project object user.username/project.name, i

I'm using the sequel gem in Ruby to connect to a sqlite database (provided by rails). I have various Users and various Projects. 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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号