Rails 3中ActiveRecord的API:.build方法 [英] Rails 3 ActiveRecord API: .build method

查看:122
本文介绍了Rails 3中ActiveRecord的API:.build方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的红宝石/回报率(境外一年),我已经注意到,有几种不同的方式回报率或Ruby里面,基本上做同样的事情。一个方法,我希望得到某种澄清,是 .build 方法。当它是有效的是使用或如何使用它在最佳光,几分事

I am fairly new to Ruby/RoR (outside of a year) and I have noticed that there are several different methods inside of RoR or Ruby that basically do the same thing. The one method I am wanting to get some sort of clarification on, is the .build method. when it is effective to use or how to use it in its best light, sorta thing.

谢谢!

推荐答案

该.build方法是用于创建基于您的模型中的has_many关系的新纪录的ActiveRecord的方法。

The .build method is an ActiveRecord method which is used to create a new record based on the has_many relationship in your model.

因此​​,可以说;

User has_many tweets

然后你可以用

Then you can use

user.tweets.build(tweet_id)

这将创建与该用户相关的tweet表一个新的鸣叫。它也将返回过多该对象。

This will create a new tweet in the tweets table associated with that user. It will also return that object too.

您可能想提出一个PARAMS tweet_id在你的说法取决于你是如何实现的应用程序。 :)

You probably want to put a params tweet_id in you argument depending on how you are implementing the app. :)

这篇关于Rails 3中ActiveRecord的API:.build方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆