如果我有数据库中表 (table1) 中的 ID 数组。有没有办法查询另一个表(表2)来选择所有记录,其中某一列的值等于表1中的ID之一。
到目前为止我的代码是:
LabQuestion.where("product_id=#{Product.where(:brand_id => brand_id).pluck(:id)}")
在此代码中,我尝试检索与某个品牌的所有产品链接的所有实验室问题。该代码不起作用,但我已尝试证明我的需求。
最佳答案
假设您已正确设置关系,则可以使用 joins
连接两个表并像这样查询它们:
LabQuestion.joins(:product).where(:products => { :brand_id => brand_id })
关于ruby-on-rails-3 - Ruby on Rails : Select from database where column equals a value from array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17679541/