1

我有一组名为“brands”的表的 ID。我想获取品牌表中每条记录的名称列,而无需使用 Brand.find(brand_id) 重新查询数据库。相反,有没有办法将数据库结果存储到变量中并查询变量?

谢谢。

4

4 回答 4

0

Brand这将返回一个包含所有s的数组brand_id

Brand.find(:all, :conditions => ["brand_id = ?", brand_id])

对于brand_ids的集合

Brand.find(:all, :conditions => ["brand_id IN (?)", [brand_id1, brand_id2]])

高温高压

于 2013-05-20T08:35:23.027 回答
0
You can write a method in your model brand.  
    def get_associated_brand  
        b = self.brand_id  
       brand = Brand.find(b).name  
   end  

From your view just call this method so that in your view you ll get brand name instead of ID like  

品牌.get_associated_brand

代替

brand.brand_id
于 2013-05-20T08:41:48.827 回答
0

关于对@Harsh Gupta 的回答的评论,也许

@filtered_brands = Brand.where(id: <some_id>)

然后,您可以在每个循环中定期访问每个品牌的所有信息。

于 2013-05-20T08:58:34.860 回答
0

如果您已经在 var @brands 中收集了记录,则可以使用

name = @brands.find {|b| b.id == brand_id}.name

不是查询

于 2013-05-20T09:06:29.160 回答