在做项目的时候,对于数据库的操作方法的胡乱调用时很可怕的,因为自己很初级,初级的不能再初级,所以初次接触rails的时候,对于里面的create,new,build等方法不是很了解,所以胡乱用了一通,导致了很多的bug,相信很多对rails接触不久的朋友也会有相同的疑惑,今天我来给大家简单的说明一下。
首先说new吧,app = App.new(params[:app]),这个是用给定的params来实例化一个app对象,注意此时只是初始化了一个对象,ruby只是在内存里面给app分配了一个内存地址,但是,这个地址还没有指定任何的有效的内存,所以接下来ruby要做的就是调用model里面的initialize函数来根据给定的参数进行初始化这个对象,并将该对象保存到堆里面。
对于new!来说,这个跟new相比,就是在save到数据库的时候,如果save失败,就会报一个错误。
对于create来说,过程大致跟new差不多,唯一的区别是,create = new + save,即create一步操作等于new和save的两部操作。
对于create!来说,类似于new! 过程大致也是跟new!差不多 区别是create! = new! + save 如果保存失败的话会返回一个错误。
对于build来说,主要的混淆的地方在于与new的区别,其实,我认为build是new的一个别名,但是用的地方可能跟new有点区别,在我看来,build大多是用于有关联关系的对象模型的时候,用来创建子model用的,比如说 Person 与 Order是一对多的关系,那么我们可以用 @persion.build()方法来创建于该person对应的orders
分享到:
相关推荐
DDKWizard is a so-called project creation wizard that allows you to create projects that use the DDKBUILD scripts from OSR (also available in the download section from this site). The wizard will give...
• Build and utilize the new APEX 5.1 Interactive Grid • Implement App Logic with APEX computations, validations, and processes • Use (automatic) built-in and manual DML to manipulate your data • ...
"解决Vue-Router进行Build无法正常显示路由页面的问题" ...本文解决了Vue-Router进行Build无法正常显示路由页面的问题,并提供了解决方法和代码示例。开发者可以根据需要选择合适的解决方法来解决这个问题。
If you already have even basic familiarity with HTML, CSS, and ... Learn to create a complete HTML5 game right now and then use your newfound skills to build your own mega-popular, addictive game.
What's new in WinZip 18.5 Build 11111: May 8th, 2014 Zip and share via instant messaging services, including Facebook Messaging, Google Talk/Google+ Hangouts, Yahoo! Messenger and Jabber Zip and ...
+ CellButtons in columns of DBGridEh and rows DBVertGridEh to create buttons in cells of the grid operating without activation InplaceEditor text editor. + Search in a mode of “Begins with” in ...
As a prerequisite, you will need some knowledge of the concepts of Android application development., What You Will Learn, Build new Android apps and libraries using Android Studio and Gradle, Migrate...
This book leads you through the essential concepts and new iOS 10 SDK and Swift 3 programming language APIs to build iPhone and iPad database driven applications using the defacto standard for data ...
Nifty是facebook公司开源的,基于netty的thrift服务端和客户端实现。 然后使用此包就可以快速发布出基于netty的高效的服务端和客户端代码。 示例: public void startServer() { // Create the handler ...
Using Apache Cordova, developers can create a new type of mobile app—a hybrid mobile app. Hybrid mobile apps actually run in an internal browser inside a wrapper created by Apache Cordova. With ...
sbt new shadaj/create-react-scala-app.g8 ... follow instructions to create your app cd my-app/ sbt dev 然后打开查看您的应用。 当您准备部署到生产环境时,请使用sbt build创建一个sbt build包 立即开始 你...
1. **Create React App**: Create React App is a popular tool for setting up a new React project with a solid build configuration. It allows you to quickly start a new React project without spending ...
Build new Android apps and libraries using Android Studio and Gradle Migrate projects from Eclipse to Android Studio and Gradle Manage the local and remote dependencies of your projects Create ...
By the end of the book, you will know everything you need to know to dive into Swift 3 and build successful projects. What you will learn Migrate a Swift 2.2 project to Swift 3 Understand the ...
FactoryFaster 通过在安全的情况下用build替换create优化 Rails 应用程序的 FactoryGirl 使用。 安装 将此行添加到“开发”组中应用程序的Gemfile中: gem 'factory_faster' 然后执行: $ bundle 用法 只处理一...
With Getting to Know Vue.js, you’ll see how to combine reusable code with custom components, allowing you to create snippets of reusable code to suit your specific business needs. You’ll also ...