I''m trying to query one table to ultimately display the relate info from another table. I have 2 tables: "posts" and "numbers". I''m trying to do a query like this: @numbers = Number.find(:all, :conditions => [ ''(user_id) = :id'', {:id => @session[''user''].id}], :include => :post) Then display it like this: <ul><% for number in @numbers do %> <li><%= link_to number.post.title, :controller => ''post'', :action => ''show'', :id => post %> Ending: <%= number.post.end_date %></li> <% end %></ul> I''m getting "Column ''user_id'' in where clause is ambiguous" From the reading I''ve been doing it seems the fact that there is a "user_id" field in both tables might be the issue. How do I work around this? Do I have to rename one of the "user_id" columns, or is there a more elegant way of doing this? -- Posted via http://www.ruby-forum.com/.
From: Jasbur <jasbur@gmail.com>> Subject: [Rails] Ambiguous clause error> I''m trying to query one table to ultimately display the > relate info from another table. I have 2 tables: "posts" and > "numbers". I''m trying to do a query like this: > > @numbers = Number.find(:all, > :conditions => [ ''(user_id) = :id'', > {:id => @session[''user''].id}], > :include => :post) > > Then display it like this: > > <ul><% for number in @numbers do %> > <li><%= link_to number.post.title, :controller => > ''post'', :action => ''show'', :id => post %> Ending: <%= > number.post.end_date %></li> > <% end %></ul>there a more elegant way of doing this? Based on your code snippet, you do not need any columns from the post table in the query''s resultset. Just taking off the :include option should do the trick. Alternatively if that cannot be done, try using the table_name.column_name notation. So using numbers.user_id should correct the situation. Ie @numbers = Number.find(:all, :conditions => [ ''(numbers.user_id) = :id'', {:id => @session[''user''].id}], :include => :post) Hope this helps, Bharat
Jasbur wrote:> I''m trying to query one table to ultimately display the relate info from > another table. I have 2 tables: "posts" and "numbers". I''m trying to do > a query like this: > > @numbers = Number.find(:all, > :conditions => [ ''(user_id) = :id'', > {:id => @session[''user''].id}], > :include => :post) > > Then display it like this: > > <ul><% for number in @numbers do %> > <li><%= link_to number.post.title, :controller => ''post'', > :action => ''show'', :id => post %> Ending: <%= number.post.end_date > %></li> > <% end %></ul> > > I''m getting "Column ''user_id'' in where clause is ambiguous" From the > reading I''ve been doing it seems the fact that there is a "user_id" > field in both tables might be the issue. How do I work around this? Do I > have to rename one of the "user_id" columns, or is there a more elegant > way of doing this?When you invlolve more than one table you usually have to specify the table name in the sql. Try this: :conditions => [ ''(numbers.user_id) = :id'', {:id => @session[''user''].id}] -- Posted via http://www.ruby-forum.com/.