join - Rails - Find with no associated records -



join - Rails - Find with no associated records -

i want select 1 user , add together associated records such kid example. kid specific place_id.

this code works, when user doesn't have kid entry got error.

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

here error:

couldn't find user id=19 [where (child.place_id = 0)]

thanks !

try where clause, since using brute sql. not produce error , either fetch or set nil:

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

ps: child.place_id or children.place_id? activerecord tends pluralize table names.

edit:

this works if there children. if want work event without children,do this:

@user = user.joins('left bring together kid on child.user_id = users.id'). where('child.place_id = ? , users.id = ?', @place_id, params[:id]). select('users.field1, child.field2 field3')

if want specific fields, add together them in select method above, provided example.

ruby-on-rails join include find associated-object

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -