`
sitoto
  • 浏览: 124215 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

find_or_initialize_by 和 find_or_create_by

    博客分类:
  • ruby
 
阅读更多
Hash 和相等性

ActiveRecord 的 hash 值是根据主键的值计算出来的,这就意味着未保存对象的 hash 值是不可靠的。同样两个 model 对象的相等比较(即==操作符)也是基于主键的,所以两个 model 对象即使它们的其他属性不一样,仍有可能被当作相等。

查找

find_by_attribute 方法后面加个 ! 号,即使用 find_by_attribute!,就能在找不到对象的时候触发一个 RecordNotFound 异常,而不是返回 nil。

find_or_initialize_by 和 find_or_create_by 也是两个好用的方法,它们在找不到对象时分别使用 new 和 create 新建一个,并用查找的属性初始化新建的对象。

find_or_initialize_by 和 find_or_create_by 在mongoid 下也是支持的。。
分享到:
评论

相关推荐

    ap6212a0_a33_sc3817r_神舟验证版本_借用nvram_ap6210这个配置文件_20170626_1834没有外层目录.7z

    # $(call find-copy-subdir-files,*,device/softwinner/polaris-common/rild/usb_modeswitch.d,system/etc/usb_modeswitch.d) PRODUCT_PROPERTY_OVERRIDES += \ ro.sw.embeded.telephony = false PRODUCT_...

    ap6212a0_a33_sc3817r_服务器验证通过_bt已经通了_wifi需要修改配置_需要再次验证_20170626_1549.7z

    # $(call find-copy-subdir-files,*,device/softwinner/polaris-common/rild/usb_modeswitch.d,system/etc/usb_modeswitch.d) PRODUCT_PROPERTY_OVERRIDES += \ ro.sw.embeded.telephony = false PRODUCT_...

    php.ini-development

    sign, or by using the None keyword: ; foo = ; sets foo to an empty string ; foo = None ; sets foo to an empty string ; foo = "None" ; sets foo to the string 'None' ; If you use constants in your ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: Added division by zero protect in method TFlexControl.MovePathSegment. - FIX: The background beyond docuemnt wasn't filled when TFlexPanel.DocClipping=True. - FIX: In "Windows ClearType" font ...

    ubuntu上在androidstudio中启动emulator闪退的解决方法

    首先,我们需要查看模拟器的信息,包括存放路径、名称和架构。可以通过查看模拟器的配置文件来获取这些信息。在本文中,模拟器的存放路径是 `/home/pengdonglin/.android/avd/4_WVGA_Nexus_S_API_24.avd`,名称是 `4...

    mysql-8.0.20-macos10.15-x86_64.tar.gz

    mysql> create user 'root'@'%' identified by 'password'; mysql> grant all privileges on *.* to 'root'@'%' with grant option; mysql> flush privileges; 如果使用 Navicat连接MySQL Server8.0版本时出现...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Another useful rule of thumb: it's typically not cost effective to inline functions with loops or switch statements (unless, in the common case, the loop or switch statement is never executed)....

    gertt点阵图读取器(opengl)

    GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window { if (height==0) // Prevent A Divide By Zero By { height=1; // Making Height Equal One...

    cmake使用glfw3.dll

    std::cerr << "Failed to create GLFW window" ; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); // OpenGL initialization and rendering code goes here... while (!...

    opengl画图程序附带源代码

    GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window { if (height==0) // Prevent A Divide By Zero By { height=1; // Making Height Equal One } glViewport(0,0,...

    MySQL主从安装部署.docx

    sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' server_id=1 max_connections=1500 lower_case_table_names=1 init_...

    SQLCE 数据库的创建

    WIN32_FIND_DATA FindFileData; // The file structure description hFind = FindFirstFile(L"\\test.sdf", &FindFileData); // 查找指定目录下的第一个文件 if (INVALID_HANDLE_VALUE != hFind) { // 失败则返回 ...

    Bochs - The cross platform IA-32 (x86) emulator

    Changes in 2.4.6 (February 22, 2011): Brief summary : - Support more host OS to run on: - Include win64 native binary in the ... [2914433] makesym.perl misses symbols by John R. Jackson [2908481] USB ...

    express-sequelize-user:在快速请求中公开序列查询函数,将当前用户注入返回的对象中

    应该与findAll,findById,findOne,findOrInitialize,findOrCreate,创建,构建一起使用。 安装 npm install express-sequelize-user --save 用法 // Bind middlewares ... app . use ( passport . initialize ( ...

    基于SIP开发软件电话的一些资源(转自YOUTOO)

    上面,我们得到了Osip的解析器开发库,下面再编译完整的Osip协议栈开发库,同样照上述方法,分别创建动态链接库工程和静态链接库工程,只是要拷的文件换成src下的osip目录下文件和include下的osip目录,...

    BobBuilder_app

    There is always room for improvement, and the ever need for faster systems compels us to create new methods of doing things. MGindex is no exception to this rule. Currently MGindex outperforms b+tree ...

    红旗linux安装oracle9.2.04

    - `(libzip.so) Unable to initialize threads: cannot find class java/lang/Thread Could not create Java VM` - **解决方案**: 降低内核参数设置。 **3.2 数据库配置工具(dbca)无法正常工作** - 安装完成后,`...

    acpi控制笔记本风扇转速

    control method attempts to create 2 objects of the same name. This once again returns AE_ALREADY_EXISTS. When this exception occurs, it invokes the mechanism that will dynamically serialize the ...

Global site tag (gtag.js) - Google Analytics