开发者

Automated way to select all columns of joined table in ActiveRecord?

开发者 https://www.devze.com 2023-03-08 11:08 出处:网络
Wondering with the upgrade to Arel, if ActiveRecord yet supports an automated way to select in the columns from joined tables, without having to explicitly list them in a select clause.

Wondering with the upgrade to Arel, if ActiveRecord yet supports an automated way to select in the columns from joined tables, without having to explicitly list them in a select clause.

G开发者_如何学Civen I'm joining the users table to posts, Would like to be able to do something like this in a more succinct manner:

  scope :select_user,
    select("posts.*, " + User.columns.map{|c| 
      "users.#{c.name} user_#{c.name}"}.join(','))

So, I want to automatically select the fields from user so that I get say

Post.joins(:user).select_user.first.user_name


I think what you actually want here is to use includes if you want to access the association's attributes after the query is complete. So the same query would be

Post.includes(:user).first.user.name

This will result in two queries, one select * from users and one select * from posts where posts.user_id in (id1, id2, id3...) (assuming post belongs_to user) which may be a performance hit in a particular situation, but may be worth it from a syntax standpoint. In some cases it may even be faster Rails :include vs. :joins.

Arel will also intelligently use a join if you add a restriction operator to the query e.g. Post.includes(:user).where('users.name = "Bo Bob"') will do one query with a join and also enable you to use the associations on the resulting Post object.


If I understand your question correctly, you can do it with 'joins':

class Post < ActiveRecord::Base
  belongs_to :user

  scope :select_user, joins(:user).where('users.name = "Mary"')
  ...
end

or with a param:

class Post < ActiveRecord::Base
  belongs_to :user

  scope :select_user, lambda {|user_name|
    joins(:user).where('users.name = ?', user_name)
  }
  ...
end
0

精彩评论

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