开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜