ruby onrails join table
I am trying to join my table in rails way.
this is 开发者_运维百科the sql query
SELECT * FROM `ads` left join `state` ON ads.state_id = states.id
Rails guides is your friend=)
http://api.rubyonrails.org/classes/ActiveRecord/Base.html
Student.joins(:schools).where(:schools => { :type => 'public' })
Student.joins(:schools).where('schools.type' => 'public' )
Or Rails ActiveRecord :joins with LEFT JOIN instead of INNER JOIN:
User.find(:all, :limit => 10,
:joins => "LEFT JOIN `user_points` ON user_points.user_id = users.id" ,
:select => "users.*, count(user_points.id)", :group =>
"user_points.user_id")
if you are using rails 5 then you can use this -
Ad.left_outer_joins(:states)
精彩评论