ruby 令人敬畏的ActiveRecord错误报告脚本。如何在Ruby脚本中使用ActiveRecord和SQLite。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 令人敬畏的ActiveRecord错误报告脚本。如何在Ruby脚本中使用ActiveRecord和SQLite。相关的知识,希望对你有一定的参考价值。

# Based on http://www.jonathanleighton.com/articles/2011/awesome-active-record-bug-reports/ 

# Run this script with `$ ruby my_script.rb`
require 'sqlite3'
require 'active_record'

# Use `binding.pry` anywhere in this script for easy debugging
require 'pry'

# Connect to an in-memory sqlite3 database
ActiveRecord::Base.establish_connection(
  adapter: 'sqlite3',
  database: ':memory:'
)

# Define a minimal database schema
ActiveRecord::Schema.define do
  create_table :shows, force: true do |t|
    t.string :name
  end

  create_table :episodes, force: true do |t|
    t.string :name
    t.belongs_to :show, index: true
  end
end

# Define the models
class Show < ActiveRecord::Base
  has_many :episodes, inverse_of: :show
end

class Episode < ActiveRecord::Base
  belongs_to :show, inverse_of: :episodes, required: true
end

# Create a few records...
show = Show.create!(name: 'Big Bang Theory')

first_episode = show.episodes.create!(name: 'Pilot')
second_episode = show.episodes.create!(name: 'The Big Bran Hypothesis')

episode_names = show.episodes.pluck(:name)

puts "#{show.name} has #{show.episodes.size} episodes named #{episode_names.join(', ')}."
# => Big Bang Theory has 2 episodes named Pilot, The Big Bran Hypothesis.

# Use `binding.pry` here to experiment with this setup.

以上是关于ruby 令人敬畏的ActiveRecord错误报告脚本。如何在Ruby脚本中使用ActiveRecord和SQLite。的主要内容,如果未能解决你的问题,请参考以下文章

Ruby on Rails 购物车错误“ActiveRecord_Associations_CollectionProxy”

令人敬畏的jQuery步骤向导插件

Ruby ActiveRecord 组和“AS”处或附近的平均“语法错误”

text 令人敬畏的蟒蛇

text 令人敬畏的反应组件

markdown 令人敬畏的名单