开发者

ruby onrails join table

开发者 https://www.devze.com 2023-01-25 07:13 出处:网络
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

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)   
0

精彩评论

暂无评论...
验证码 换一张
取 消