如何在 Sequel Ruby 模型中获取一个表中而不是另一个表中的所有记录
这个想法是返回 table_1 模型的模型实例,其中 table_1 的 id 不存在于 table_2 中,而 another_id 具有值。这样我就可以使用这些实例和其他地方包含的数据
def self.my_method(another_id:)
result = DB["SELECT t1.* FROM table_1 t1 WHERE t1.id NOT IN (SELECT t2.id
FROM table_2 t2 WHERE t2.another_id = #{another_id})"]
result_array = []
result.each do |row|
result_array << self.find(id: row[:id])
end
result_array
end
我有 table_1 和 table_2 的模型
这可以工作,但感觉非常笨重并且可能非常低效,因为对于返回的每个 id 我正在重建每个 table_1 模型的实例再次查看数据库。
如何使用模型/数据集上可用的方法以典型的 Sequel 方式完成此操作。从我的角度来看,我正在努力解决如何将各种语法组合在一起来做到这一点
The idea is to return model instances of the table_1 model where the id's of table_1 do not exist in table_2 where another_id has a value. This is so I can use those instances and the data contained within elsewhere
def self.my_method(another_id:)
result = DB["SELECT t1.* FROM table_1 t1 WHERE t1.id NOT IN (SELECT t2.id
FROM table_2 t2 WHERE t2.another_id = #{another_id})"]
result_array = []
result.each do |row|
result_array << self.find(id: row[:id])
end
result_array
end
I have models for both table_1 and table_2
This works but it feels very clunky and probably very inefficient as for every id that is returned I am rebuilding an instance of each table_1 model by looking at the database again.
How would this be done in a typically Sequel way using the methods available on the Model/Dataset. From my viewpoint I am struggling with how I pull together the various pieces of syntax to do this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论