Rails的寻找方法 - 选择列从":包括"表参数 [英] Rails find method - select columns from ":include" table parameter
问题描述
下面find方法将返回所有记录,但不包括场地表列。有没有一种方法,我可以修改下面包括场馆列?我试过:选择=> venues.id
和:选择=> 场地。*
,都无济于事。也许这是一个限制与轨道寻找方法。可查询被改写为可能Item.where包括所有列?
The below find method returns all records but doesn't include venues table columns. Is there a way I can modify the below to include columns from venues? I tried :select => 'venues.id'
, and :select => 'venues.*'
, to no avail. Perhaps this is a limitation with the rails find method. Can the query be rewritten as perhaps "Item.where" to include all columns?
@vehicles = Vehicle.find(:all,
:select => '*',
:joins => [:vehicle_status, :category, :user],
:include => [:venue],
:conditions => ["vehicles.client_id = ? ", current_user.client_id]
)
我使用的是轨道3.2.11。
I'm using rails 3.2.11.
另外,我当我使用:选择=> '。*的车辆,场地*
,并得到它显示了被选择的场地领域控制台上的后续查询,但它们不会出现在返回的JSON:
Also, my when I use :select => 'vehicles.*, venues.*'
and get the follow query on the console where it shows venues fields being selected, but they do not appear in the returned json:
SELECT "vehicles"."id" AS t0_r0, ... "vehicles"."created_at" AS t0_r33,
"vehicles"."updated_at" AS t0_r34, "venues"."id" AS t1_r0, "venues"."client_id" AS t1_r1,
"venues"."venue_name" AS t1_r2, "venues"."created_at" AS t1_r3, "venues"."updated_at" AS t1_r4
FROM "vehicles" INNER JOIN "vehicle_statuses" ON "vehicle_statuses"."vehicle_status_code" =
"vehicles"."status_code" INNER JOIN "categories" ON "categories"."id" =
"vehicles"."category_id" INNER JOIN "users" ON "users"."id" = "vehicles"."created_at_user_id"
LEFT OUTER JOIN "venues" ON "venues"."id" = "vehicles"."venue_id" WHERE (vehicles.client_id =
500 )
不过,如果我使用:选择=> '*'
,控制台显示两个查询:一个用于车辆表,另一个是场地:场地负载(0.3ms)选择场地* FROM场地WHERE场地。 ID在(5000)
However, if I use :select => '*'
, the console shows two queries: one for the vehicles table, the other for venue: Venue Load (0.3ms) SELECT "venues".* FROM "venues" WHERE "venues"."id" IN (5000)
推荐答案
您应该尝试
format.json { render json: @vehicles, :include => :venue }
您也可以做一个加入
@vehicles = Vehicle.where(client_id: current_user.client_id).joins(:category, :vehicle_status).includes(:venue)
场地或根据的has_many
场地或 HAS_ONE
。欲了解更多信息查找的东西在这里 http://guides.rubyonrails.org/active_record_querying.html
venue or venues depending on has_many
or has_one
. For further information lookup stuff here http://guides.rubyonrails.org/active_record_querying.html
这篇关于Rails的寻找方法 - 选择列从":包括"表参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!