从数据库进行查询时如何返回模型父值

时间:2022-10-05 12:36:38

So I have a model called Image that belongs_to :user. Each user has a first and last name.

所以我有一个名为Image的模型,即belongs_to:user。每个用户都有名字和姓氏。

I have a flash app that I am returning a json object back to of Images.

我有一个Flash应用程序,我将json对象返回到Images。

the service I will be calling on the Images controller would look something like this

我将在图像控制器上调用的服务看起来像这样

def getimages
    @images = Image.all
    render :json => @images

  end

My json would look something like this

我的json看起来像这样

[{"image":{"created_at":"2011-01-22T19:04:30Z","img_path":"assets/img/bowl_93847566_3_0.png","updated_at":"2011-01-22T19:04:30Z","id":9,"user_id":3}}]

what I would like to do is also include the users first and last name with in the image object that gets passed back.

我想要做的还包括在传递回来的图像对象中的用户名和姓。

once I have an image object I am able to do something like image.user.first_name but I am not clear how I would return something like an array of image objects and include the user along with it.

一旦我有了一个图像对象,我就可以做像image.user.first_name这样的事情,但我不清楚如何返回像图像对象数组这样的东西并包含用户。

what would be great is if I could get my array of images to look like the following.

如果我能让我的图像阵列看起来如下,那将是多么美妙的事情。

[{"image":{"created_at":"2011-01-22T19:04:30Z","img_path":"assets/img/bowl_93847566_3_0.png","updated_at":"2011-01-22T19:04:30Z","id":9,"user_id":3, "first_name":"Matthew", "last_name":"Wallace"}}]

I am thinking this may include adding some kind of model method or somthing that I am not familiar with.

我想这可能包括添加某种我不熟悉的模型方法或某种东西。

What would be the best practice for achieving this?

实现这一目标的最佳做法是什么?

2 个解决方案

#1


1  

You could:

render :json => @images.to_json(:include => :users)

See http://apidock.com/rails/ActiveRecord/Serialization/to_json (and http://apidock.com/rails/Array/to_json shows it works on Arrays). Finally, http://apidock.com/rails/ActionController/Base/render describes using to_json in a json render as optional and not required, which implies it should cause no harm (I couldn't see another way to pass the required options in).

请参阅http://apidock.com/rails/ActiveRecord/Serialization/to_json(http://apidock.com/rails/Array/to_json显示它适用于阵列)。最后,http://apidock.com/rails/ActionController/Base/render描述了在json渲染中使用to_json是可选的而不是必需的,这意味着它应该不会造成任何伤害(我无法看到另一种传递所需选项的方法在)。

Perhaps cleaner json:

也许清洁json:

render :json => @images.to_json(:include => { :user => { :only => [:first_name, :last_name] } })

#2


0  

Besides the answer provided by @apneadiving, you can also override the Image's to_json method and return a string containing whatever JSON you need.

除了@apneadiving提供的答案之外,您还可以覆盖Image的to_json方法并返回包含所需JSON的字符串。

#1


1  

You could:

render :json => @images.to_json(:include => :users)

See http://apidock.com/rails/ActiveRecord/Serialization/to_json (and http://apidock.com/rails/Array/to_json shows it works on Arrays). Finally, http://apidock.com/rails/ActionController/Base/render describes using to_json in a json render as optional and not required, which implies it should cause no harm (I couldn't see another way to pass the required options in).

请参阅http://apidock.com/rails/ActiveRecord/Serialization/to_json(http://apidock.com/rails/Array/to_json显示它适用于阵列)。最后,http://apidock.com/rails/ActionController/Base/render描述了在json渲染中使用to_json是可选的而不是必需的,这意味着它应该不会造成任何伤害(我无法看到另一种传递所需选项的方法在)。

Perhaps cleaner json:

也许清洁json:

render :json => @images.to_json(:include => { :user => { :only => [:first_name, :last_name] } })

#2


0  

Besides the answer provided by @apneadiving, you can also override the Image's to_json method and return a string containing whatever JSON you need.

除了@apneadiving提供的答案之外,您还可以覆盖Image的to_json方法并返回包含所需JSON的字符串。