1

I am using Rail 3 and I am finding it very hard to do a join of two tables AND get access to data from both of them in the view. I only get access to one of the two. What is wrong with the below code? How should it look like?

Please note that I am using Rails 3.

@contacts = Profile.where("profiles.id = ?", @profile).includes(:contacts).order("lastname ASC")

I have also tried something like this

@contacts = Profile.joins('LEFT OUTER JOIN contacts ON contacts.friend_id = profiles.id').where("profiles.firstname LIKE :input OR profiles.lastname LIKE :input",{:input => "#{params[:keyword]}%"}).where("contacts.profile_id = #{params[:profile_id]}")
halfer
  • 19,824
  • 17
  • 99
  • 186
Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175

1 Answers1

2

You are missing the select method

See this question solution: Rails 3 - select with Include? It works similar with a JOIN, allowing you to select fields from both tables but the results will be in a Profile object with Contact fields as virtual columns.

Community
  • 1
  • 1
Leonel Galán
  • 6,993
  • 2
  • 41
  • 60