`

rails activerecord join查询 include 和 find_or_create find_or_initialize

阅读更多
Student.joins(:schools).where(:schools => { :category => 'public' })
Student.joins(:schools).where('schools.category' => 'public' )



# No 'Summer' tag exists
Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer")

# Now the 'Summer' tag does exist
Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer")

# Now 'Bob' exist and is an 'admin'
User.find_or_create_by_name('Bob', :age => 40) { |u| u.admin = true }


小差别在于是否创建


winter = Tag.find_or_initialize_by_name("Winter")
winter.persisted? # false


productproperty = ProductProperty.find_or_create_by_product_id(product.id) { |u| u.property_id => property_id, u.value => d[descname] } )


conditions = { :product_id => product.id, 
               :property_id => property.id,
               :value => d[descname] }

pp = ProductProperty.find(:first, :conditions => conditions) || ProductProperty.create(conditions) 


down vote
	

If you want to search by multiple attributes, you can use "and" to append them. For example:

productproperty = ProductProperty.find_or_create_by_product_id_and_property_id_and_value(:product_id => product.id, :property_id => property.id, :value => d[descname])


dynamic finder style
分享到:
评论

相关推荐

    使用ActiveRecord验证密码强度_Ruby_下载.zip

    在Ruby开发中,ActiveRecord是Rails框架的一部分,用于处理数据库操作和对象关系映射(ORM)。在用户注册或账户管理时,确保用户设置的密码具有足够的强度是非常重要的,以防止轻易被破解。这个资料包“使用...

    RestFul_Rails_Dev_pdf_v_0.1.zip

    本资料“RestFul_Rails_Dev_pdf_v_0.1.zip”包含了《RESTful Rails Development》的翻译版,将深入探讨如何在Rails中实现RESTful的设计模式。 首先,RESTful设计的核心概念是资源(Resources)。在Rails中,资源...

    active_record_patch_first_or_create

    AtomicFirstOrCreate ActiveRecord first_or_create替代方案,它在RecordNotUnique异常上重试。 first_or_create本身不能保证唯一性...正如Rails团队所记录的那样,诸如first_or_create或find_or_create方法不是原子

    to_xls-rails:将Rails ActiveRecord或Mongid数据导出到Excel文件

    数组元素支持对象:ActiveRecord,Mongid,哈希。 在您的Gemfile中: gem 'to_xls-rails' # Last officially released gem # gem "to_xls-rails", :git => "git://github....

    Rails3的ActiveRecord 查询API.doc

    Rails 3.1 及以后版本的 ActiveRecord 查询API发生了重大变化,主要目的是为了提供更清晰、更可维护的代码,并且提高性能。在 Rails 2.x 中,许多使用哈希参数的查询方法如 `:conditions`, `:include`, `:joins` 等...

    Rails_Recipes_with_Source_Code

    12. **缓存(Caching)**:Rails提供了页面、片段、查询和Memcached等多种缓存策略,书中可能介绍如何提高应用性能。 13. **国际化(i18n)**:Rails支持多语言应用,书中可能讲解如何配置和使用i18n系统。 14. **...

    rails_admin_acts_as_list:rails_admin插件以对记录进行排序

    介绍插件,用于对记录进行排序(使用 gem)安装要启用rails_admin_acts_as_list,请将以下内容添加到您的Gemfile : gem 'rails_admin_acts_as_list'gem 'rails_admin' 重要提示: rails_admin_acts_as_list之前必须...

    Ruby-railsor支持并添加语法糖或者查询方法在Rails345中

    `rails_or` 是一个Rails的扩展库,它为ActiveRecord(Rails的ORM框架)添加了额外的查询语法糖,使得数据库查询更加简洁和高效。这个库的目标是进一步提升开发者在编写查询时的体验,通过引入新的查询方法来减少代码...

    rails_or:用于在Rails 5、6中编写OR Query的更简洁语法。还为Rails 3和4添加#or支持。

    RailsOr rails_or是一个Ruby Gem,供您编写更整洁的OR查询。 它将尽可能使用Rails 5中添加的内置方法or方法,因此您不必担心它将影响active_model 。 否则,它将为Rails 3和Rails 4实现or方法。支持Ruby2.2〜2.7 ...

    rails_model_test_hello_world

    自述文件版本和设置$ ruby -vruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18]$ rails -... $ rails new rails_model_test_hello_world -T -m ~/rtfb_template.rb$ cd rails_model_test_hello_world$ rail

    使用Rails4Devise和Grape验证API_JavaScript_CSS_下载.zip

    在压缩包中的"authenticating_api_rails_devise-master"可能是一个Git仓库的名称,表明这个项目是用Git版本控制系统管理的,"master"分支通常代表开发的主要分支。这个目录可能包含以下文件和目录结构: - Gemfile...

    Complex Rails system_Rails_优化_

    Rails,作为一个强大的Web开发框架,虽然提供了丰富的功能和便利性,但如果不进行适当的优化,可能会导致应用程序响应时间变慢,资源消耗过大,甚至影响用户体验。本篇文章将深入探讨如何针对Rails系统的复杂架构...

    ActiveRecord简单实例_activerecord.zip

    在Ruby on Rails框架中,ActiveRecord是一个至关重要的组件,它负责模型(Model)与数据库之间的交互。本实例将深入探讨ActiveRecord的基本用法,帮助理解如何在实际开发中有效地运用这个强大的工具。 首先,让我们...

    用于测量 gem 的 Rails 适配器,在 Ruby 和 Rails 中封装测量值及其单位_Ruby_代码_相关文件_下载

    它提供了 ActiveRecord 适配器,用于保存和检索测量值及其单位和模型验证。 用法 活动记录 列应具有_valueand_unit后缀,并且是DECIMALand VARCHAR,并且接受默认值。支持自定义用于保存单位的列,详细信息见下文。...

    Ruby on Rails Guides v2 - Ruby on Rails 4.2.5

    - **方法**:使用Active Record的方法来进行数据库查询,如`find`、`where`、`joins`等。 - **优化**:为了提高查询效率,可以使用预加载(`includes`)和延迟加载(`references`)等技术减少数据库交互次数。 #### 五...

    Rails里给文件上传添加progress_bar

    File.open(Rails.root.join('public', 'uploads', file.original_filename), 'wb') do |f| f.write(file.read) end # 或者将文件信息存入数据库 # @upload = Upload.new(file: file) # @upload.save end ...

    W5D5:W5D5 Classwork-Rails ActiveRecord查询

    在“W5D5: W5D5 Classwork - Rails ActiveRecord查询”这个主题中,我们将深入探讨如何使用ActiveRecord进行有效的数据库查询,以提高开发效率。 一、ActiveRecord简介 ActiveRecord是Rails中的一个ORM(对象关系...

    activerecord-commentator:将caller_location(文件名,行号和方法名)添加为SQL注释

    ActiveRecord :: Commentator将caller_location (文件名,行号和方法名)添加为调用SQL语句SQL注释。 安装 将此行添加到您的应用程序的Gemfile中: gem 'activerecord-commentator' 然后执行: $ bundle 或将...

    Ruby-on-Rails-rails.zip

    Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zipRuby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zipRuby_on_...

    rails查询学习笔记

    1. **ActiveRecord查询接口**:这是Rails中最基础的查询方式,如`Model.find(id)`用于根据ID获取记录,`Model.where(condition)`用于根据条件筛选记录,`Model.order(column)`用于排序,`Model.includes(:...

Global site tag (gtag.js) - Google Analytics