0

I want to select one user and add to it associated records such as child for this example. But only the child with a specific place_id.

This code works, but when the user doesn't have any child entry I got an error.

  @user = User.includes(:child).find(params[:id],
                                             :conditions => ["child.place_id = ?", @place_id])

Here is the error:

Couldn't find User with id=19 [WHERE (child.place_id = 0)]

Thanks !

user2037696
  • 1,055
  • 3
  • 16
  • 33

1 Answers1

1

Try where clause, since you are already using brute SQL. This will not produce error and will either fetch or set to nil:

@user = User.includes(:child).
             where("users.id=? AND child.place_id = ?", 
                    params[:id],@place_id).first

PS: Is it child.place_id or children.place_id? ActiveRecord tends to pluralize table names.

EDIT:

This only works if there are children. If you want it to work event without children,do this:

@user = User.joins('LEFT JOIN child on child.user_id = users.id').
             where('child.place_id = ? AND users.id = ?', @place_id, params[:id]).
             select('users.field1, child.field2 as field3')

If you want specific fields, add them in select method above, which is provided as an example.

Ruby Racer
  • 5,690
  • 1
  • 26
  • 43
  • I already tried your solution :) but instead of the error I got "null", not even the user information. It's "child" it's just for the example. Thanks for you help btw – user2037696 Oct 13 '14 at 20:38
  • In that case, if you have a hands-on-example, I would appreciate seeing it in order to be able to provide valid feedback. – Ruby Racer Oct 13 '14 at 20:40
  • I don't really have any other example to provide :/ I'm stuck. I tried so many query, but still the same issue... – user2037696 Oct 13 '14 at 23:31
  • With your second query I get all the users, I want to be able to select only one specific user. Same as your first query. – user2037696 Oct 14 '14 at 14:39
  • that returns me a list of the same user. – user2037696 Oct 14 '14 at 14:49
  • my model is very simple, according to your query you exactly understood my problem. It's just that the query is not right. I have a parent table and a child table, the child table has a field parent_id and age. I want the parent with the id=5 including only his children with the age = 15 for example. Thanks for you help. So I only want to get the user with the id = 15, including his child if exist. – user2037696 Oct 14 '14 at 17:34
  • Wait.. if it is so, then what do you mean "returns you a list"? This query should do exactly what you are describing... A user with all his children, each row containing the user and the child. Then, it will filter out only the child with place_id you provide... If it's more than one, it will be more than one rows... – Ruby Racer Oct 14 '14 at 18:23
  • That returns me a list of the same user for example user with id 43 [{id: 43, child : {}}, {id: 43, child: {}}, ... ] – user2037696 Oct 14 '14 at 19:15