在rails查询中使用select语句

时间:2022-11-30 07:43:31

I have three tables,

我有三张桌子,

products has 3 columns, let them be product_id,retailer_id,id

产品有3列,让它们是product_id,retailer_id,id

product_urls has 2 columns, url_id,id

product_urls有2列,url_id,id

urls has 2 columns, url_id, absolute_url

urls有2列,url_id,absolute_url

All the associations has been done in models.

所有协会都是在模型中完成的。

Products & product_urls are joined on id = id (Products "has many" relationship with product_urls)

产品和product_urls在id = id上加入(产品“与product_urls有很多关系”)

product_urls & urls are joined on Url_id = id (product_urls "belongs to" relationship with urls)

product_urls和url在Url_id = id上加入(product_urls“属于”与url的关系)

products & urls have "has many relationship"

产品和网址“有很多关系”

I am trying to write a query, which selects A,B,C column from Table1 and X from Table3.

我正在尝试编写一个查询,它从Table1中选择A,B,C列,从Table3中选择X.

My query is:

我的查询是:

  • Product.joins(:urls).select(:product_id,:retailer_id,:absolute_url).where(:id=>100, :urls => {:url_id=>100})
  • Product.joins(:urls).select(:product_id,:retailer_id,:absolute_url).where(:id => 100,:urls => {:url_id => 100})

I am able to execute the query without any error, but I am not able to get data for X. when I check for sql query it is

我能够执行查询没有任何错误,但我无法获取X的数据。当我检查sql查询它是

  • select products.product_id, products.retailer_id, products.absolute_url ........ is executed.
  • 选择products.product_id,products.retailer_id,products.absolute_url ........执行。

Please help me how to fetch value from urls??

请帮我如何从网址获取价值?

1 个解决方案

#1


1  

Try the below code

请尝试以下代码

Product.joins(:urls).select("products.product_id,products.retailer_id,urls.abso‌​lute_url").where(:id=>100, :urls => {:url_id=>100})

#1


1  

Try the below code

请尝试以下代码

Product.joins(:urls).select("products.product_id,products.retailer_id,urls.abso‌​lute_url").where(:id=>100, :urls => {:url_id=>100})