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
分享到:
相关推荐
Rails_Recipes_with_Source_Code
AtomicFirstOrCreate ActiveRecord first_or_create替代方案,它在RecordNotUnique异常上重试。 first_or_create本身不能保证唯一性...正如Rails团队所记录的那样,诸如first_or_create或find_or_create方法不是原子
数组元素支持对象:ActiveRecord,Mongid,哈希。 在您的Gemfile中: gem 'to_xls-rails' # Last officially released gem # gem "to_xls-rails", :git => "git://github....
使用Rails4Devise和Grape验证API_JavaScript_CSS_下载
介绍插件,用于对记录进行排序(使用 gem)安装要启用rails_admin_acts_as_list,请将以下内容添加到您的Gemfile : gem 'rails_admin_acts_as_list'gem 'rails_admin' 重要提示: rails_admin_acts_as_list之前必须...
Rails3的ActiveRecord 查询API.doc
应用Rails进行REST 开发 ,翻译自《RESTful Rails Development》
RailsOr rails_or是一个Ruby Gem,供您编写更整洁的OR查询。 它将尽可能使用Rails 5中添加的内置方法or方法,因此您不必担心它将影响active_model 。 否则,它将为Rails 3和Rails 4实现or方法。支持Ruby2.2〜2.7 ...
自述文件版本和设置$ 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
它提供了 ActiveRecord 适配器,用于保存和检索测量值及其单位和模型验证。 用法 活动记录 列应具有_valueand_unit后缀,并且是DECIMALand VARCHAR,并且接受默认值。支持自定义用于保存单位的列,详细信息见下文。...
ActiveRecord :: Commentator将caller_location (文件名,行号和方法名)添加为调用SQL语句SQL注释。 安装 将此行添加到您的应用程序的Gemfile中: gem 'activerecord-commentator' 然后执行: $ bundle 或将...
您可以将此版本的~> 0.2与Ruby 2.2、2.3和/或Rails 4.0、4.1一起使用 用法 Post.first.to_sql 为了方便,干净地使用自定义字符串,您可以使用内置优化功能: using PpSql::ToSqlBeautifyRefinement 或者如果您需要...
Rails 的英文脏话过滤器 这个插件将允许您使用基本替换或字典术语过滤亵渎。
一个从图像生成引导配色方案的 Rails 应用程序
css和图片 博文链接:https://hideto.iteye.com/blog/197517
效果展示: https://github.com/zaru/action_cable-ika-game-sample/raw/master/ika2.gif?raw=true
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
Ruby on Rails 的应用程序,用于管理来自诸如 openSUSE Travel Support Program、GNOME 的 Conference Travel Subsidy Program 或 KDE eV Travel Cost Reimbursement 计划等自由软件组织的旅行帮助计划的请求和报销...
本文将介绍浅谈Ruby on Rails中的include和extend。include主要用来将一个模块插入到一个类或者其它模块。extend用来在一个对象中引入一个模块,这个类从而也具备了这个模块的方法。
gem 'rails_admin_content_builder' 运行生成器并进行迁移 rails g rails_admin_content_builder rake db:migrate 在app / assets / application.scss中添加样式 * = require rails_admin_content_builder 用法 ...