Tag: ActiveRecord
book = Book.find(999)
book = Book.find_by_id(999) book = Book.find_by_name('おもしろい本') book = Book.find_by_name!('おもしろい本') #=>例外発生たい場合
book = Book.find_by(id: 999) book = Book.find_by(name: 'おもしろい本') book = Book.find_by!(name: 'おもしろい本') #=>例外発生させたい場合 book = Book.find_by(name: 'おもしろい本', price: 100) #=>2項目のAnd検索。
book = Book.where("title=?", title).first
books = Book.where("title='abc'")
books = Book.where("title=? AND price=?" title, price)
books = Book.where(title: 'abc') books = Book.where('title' => 'abc') books = Book.where(title: 'abc', author: 'def')
users = User.includes(:address) users.each do |user| user.address.city end User.includes(:posts).where('posts.name = ?', 'example').references(:posts)
Book.order(:created_at)
Book.order(created_at: :desc) Book.order(created_at: :asc) Book.order("created at DESC") Book.order("title DESC, created_at ASC")
Book.order(:created_at).limit(10)
User.where(age:25).update_all(hobby:'game')
@product.save(validate: false)
Manager.where(:manager_level => 5).destroy_all
item.new_record?
item.persisted?
class OldPerson < ActiveRecord::Base self.table_name = 'person' self.inheritance_column = :old_type self.establish_connection(:adapter=>"mysql2",:host=>"localhost",:username=>$dbuser,:password=>$dbpasswd,:data\ base=>$olddbname) end class Person < ActiveRecord::Base self.inheritance_column = :old_type self.establish_connection(:adapter=>"mysql2",:host=>"localhost",:username=>$dbuser,:password=>$dbpasswd,:data\ base=>$newdbname) end
class OldPerson < ActiveRecord::Base self.table_name = 'person' self.inheritance_column = :old_type end
innodb_buffer_pool_size = 512M innodb_log_file_size = 128M
require 'logger' ActiveRecord::Base.logger = Logger.new(STDOUT)
class Device < ActiveRecord::Base def prepare full_name = (!show_version || version.nil?)? name : name + " " + version.to_s end end
class Device < ActiveRecord::Base def prepare self.full_name = (!show_version || version.nil?)? name : name + " " + version.to_s end end
I18n.enforce_available_locales = false