【发布时间】:2014-08-13 13:13:06
【问题描述】:
我想在索引方法中呈现 json,该方法连接来自 Rails 中的外键相关表的数据。我有一个以 user_id 作为外键的模型。
class Batch < ActiveRecord::Base
belongs_to :user
然后我将呈现该表并公开 user_id 字段。
def index
panels = Batch.where("user_id = #{current_user.id} or (user_id <> #{current_user.id} and public_panel = true)")
render json: panels, only: [:id, :description, :name, :public_panel, :updated_at, :user_id], root: false
end
我想做的是以某种方式公开用户名,该用户名位于 Users 模型上。即:user_id.user_name
编辑
通过阅读文档,我认为我需要使用 include 但也想为其中一个字段设置别名。我在两个表中都有一个名为 name 的字段。
这个 include 有问题
def index
panels = Batch.include([:users]).where("user_id = #{current_user.id} or (user_id <> #{current_user.id} and public_panel = true)")
render json: panels, only: [:id, :name, :description, :public_panel, :updated_at, :user_id], root: false
end
EDIT2
谢谢@Paritosh Piplewar ...我在语法上遇到了一些错误。更复杂的是,我所关注的字段是 user.name,而不是 user.user_name。这会和 batches.name 冲突,所以我需要给它起别名。
Started GET "/api/batches" for 10.0.2.2 at 2014-08-13 15:39:03 +0200
SyntaxError (/home/assay/assay/app/controllers/api/batches_controller.rb:11: syntax error, unexpected tLABEL
user: { only: :first_name },
^
/home/assay/assay/app/controllers/api/batches_controller.rb:11: syntax error, unexpected ',', expecting keyword_end
user: { only: :first_name },
^
/home/assay/assay/app/controllers/api/batches_controller.rb:12: syntax error, unexpected ',', expecting keyword_end):
编辑 3
原来的问题已经回答了,但是返回的json是这样的
data: [{"id"=>1306, "updated_at"=>Wed, 13 Aug 2014 12:37:23 UTC +00:00, "description"=>"asc", "user_id"=>1, "public_panel"=>true, "user"=>{"name"=>"Joe Bloggs"}}, {"id"=>1307,
这一点导致我的 Angular.js 前端出现问题。
"user"=>{"name"=>"Joe Bloggs"}}
【问题讨论】:
-
您可以在批处理模型中委托用户名
-
谢谢@AnilMaurya 我该怎么做?
-
旁注:在数据库查询中使用字符串插值可能非常不安全。而是使用
?替换来正确引用您的值以防止 SQL 注入,如下所示:where("user_id = ? or (user_id <> ? and public_panel = true)", current_user.id, current_user.id)
标签: ruby-on-rails