Ruby-on-rails Rails 3 - 选择包含?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4047833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 3 - select with Include?
提问by sscirrus
Here is a nested select with include:
这是一个带有 include 的嵌套选择:
@items = Item.where("complete = ?", true).includes( :manufacturer, {:order=>[:supplier, :agent] })
This is a taxing query as it pulls 1000s of rows of data from all the above included tables.
这是一个繁重的查询,因为它从上面包含的所有表中提取了 1000 行数据。
How can I get the query to only select specific fields?
如何让查询仅选择特定字段?
- user.name, user.created_at
- order.created_at
- supplier.name
- agent.name
- manufacturer.name
- user.name, user.created_at
- order.created_at
- 供应商名称
- 代理名称
- 生产商名称
采纳答案by Patrick Klingemann
There is a select method in ARel, but you must use the correct table names (i.e. plural and beware if you have polymorphic models or if you're using set_table_name or some other similar non-standard practice)
ARel 中有一个 select 方法,但您必须使用正确的表名(即复数,如果您有多态模型或使用 set_table_name 或其他类似的非标准做法,请注意)
@items = Item.
select('users.name', 'users.created_at', 'orders.created_at', 'suppliers.name', 'agents.name', 'manufacturers.name').
where(:users => { :fulfilled => true }).
includes(:orders => [:supplier, :agent], :manufacturer)
"We can use select with joins not includes" - @Bhavesh_A_P
“我们可以使用 select 和 joins not includes” - @Bhavesh_A_P
Note:
笔记:
As @Bhavesh_A_P pointed out above, selectwith includesdoes not produce consistent behavior. It appears that if the included association returns no results, selectwill work properly, if it returns results, the select statement will have no effect. In fact, it will be completely ignored, such that your select statement could reference invalid table names and no error would be produced. selectwith joinswill produce consistent behavior.
正如@Bhavesh_A_P 上面指出的那样,selectwithincludes不会产生一致的行为。看来,如果包含的关联没有返回结果,select会正常工作,如果返回结果,select 语句将不起作用。事实上,它会被完全忽略,这样你的 select 语句可能会引用无效的表名,并且不会产生错误。 selectwithjoins将产生一致的行为。
回答by Bhaveshkumar
Actually we can't use select with includes. It can only be used with joins.
实际上,我们不能将 select 与包含一起使用。它只能与连接一起使用。
回答by Byofuel
The problem is not solved by including a call to the 'select' method in the chain. In a similar ActiveRecord::Relation we built, calling 'includes' seems to override any call to 'select'.
通过在链中包含对“select”方法的调用并不能解决该问题。在我们构建的类似 ActiveRecord::Relation 中,调用 'includes' 似乎覆盖了对 'select' 的任何调用。
scope :active, where("hired_on < ? AND (separated_on > ? OR separated_on IS NULL)", Time.now, Time.now )
scope :with_role, lambda {|roles| includes(:team_member_roles).where(:team_member_roles => {:role => roles } ) }
scope :with_site_code, lambda {|site_codes| includes(:team_member_sites).where(:team_member_sites => {:site_code => site_codes } ) }
TeamMember.select("team_members.email, team_members.first_name, team_members.last_name").active.with_site_code(params[:site_code]).with_role(["senior_editing", "senior_and_reg_editing"])
As shown, the query selects all columns.
如图所示,查询选择所有列。
When the 2 scopes use 'joins' instead of 'includes', the query works: only the 3 specified columns are selected.
当 2 个范围使用 'joins' 而不是 'includes' 时,查询有效:仅选择 3 个指定的列。
回答by Moin Haidar
Item.where("fulfilled = ?", true)
.includes({:orders =>[:suppliers, :agents]}, :manufacturers)
.select("users.name, users.created_at, orders.created_at, suppliers.name, agents.name").
.order('orders.created_at DESC')

