How to manually join two different table with different attribute name in Ruby on Rails controller
<p>I am currently making a website that runs on Ruby on Rails. I am facing some issues while I was trying to join two tables, <strong><em>Rates</em></strong> and <strong><em>Locations</em></strong>, that I have with two different attributes name.</p>

<p><strong>Rates:</strong> <code>id rater_id rateable_id</code> (and a few more attributes in this table)</p>

<p><strong>Locations:</strong> <code>id title body user_id</code> (and a few more attributes in this table)</p>

<p>Here is the query that I am trying to do in SQL.</p>

<pre><code>SELECT * 
FROM rates, locations
WHERE rates.rater_id = locations.user_id AND rates.rateable_id = locations.id
</code></pre>

<p>I have read the official active record documents that provided by rubyonrails.org. I have tried doing these, but it does not work. Here is the code that I am trying to implant in <code>app\controllers\users_controller.rb</code></p>

<pre><code>@join_rating = Rate.joins(:locations).where("rates.rateable_id = locations.id AND rates.rater_id = locations.id")
@all_rating = @all_rating.where(rater_id: @user)
@count_all_rating = @all_rating.count
</code></pre>

<p><code>@join_rating</code>, is trying to join the attributes with different names. </p>

<p><code>@all_rating</code>, is trying to filter which location to show using the user ID</p>

<p><code>@join_rating</code>, is trying to calculate the total numbers of locations that are rated by the user </p>

<p>Assume that everything is setup correctly and the only error is in the query that I am trying to do, how should I rewrite the statement so that I am able to show the locations that the user has rated using <code>@all_rating</code>.</p>

<p>Thank you!</p>