In a Rails app I’m setting up nested routes
resource Country do
resource State
resource City
resource User
resource Post
end
I now want to display users that have posted within a country at ../country/1/users
In the User controller I have:
def index
@user = User.includes(:posts, :city, :state, :country)
@user = @users.where("states.country_id = ?", params[:country_id]) if params[:country_id]
@active_users = @users.where('posts_count > ?', 0).ranked_scope
end
I’m, getting:
PG::Error: ERROR: column reference "posts_count" is ambiguous
LINE 1: ...untry_id" WHERE (states.country_id = '1') AND (posts_co...
I’m not sure if this is because:
- There is a
posts_countcolumn on User, City, State, Country models and the query does not know which on to use. If so, what is the correct syntax to specify the table? - I’m stringing together two where clauses incorrectly.
- Something else….?
I’d appreciate any suggestions or ideas.
OK, I eventually got to the bottom of this. The following query was incorrect
Post belongs to User, and Post has one Country through State. Without a direct relationship in one direction between USer and Country, I modified the query to
(and double checked all my model relationships were properly configured).
Works!
The error message was not very helpful in tracking this down though!