1

I have three tables,

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

product_urls has 2 columns, url_id,id

urls has 2 columns, 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 & urls are joined on Url_id = id (product_urls "belongs to" relationship with urls)

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.

My query is:

  • 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

  • select products.product_id, products.retailer_id, products.absolute_url ........ is executed.

Please help me how to fetch value from urls??

12
  • Try Table1.joins(:table3).select("A,B,C,table3.X").where(:D=>100, :table3 => {:Y=>100}) Commented Jul 31, 2015 at 17:05
  • Hi, Thanks for the reply. But, when I am using that it is erroring out as column is ambiguous Commented Jul 31, 2015 at 17:07
  • How you are using? Also Please post the real names of the tables and columns so that it would be easy to answer. Commented Jul 31, 2015 at 17:09
  • I modified the column names, Please have a look at this Commented Jul 31, 2015 at 17:23
  • 1
    Try this Product.joins(:urls).select("products.product_id,products.retailer_id,urls.absolute_url").where(:id=>100, :urls => {:url_id=>100}) Commented Jul 31, 2015 at 17:30

1 Answer 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})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.