[RUBY-ON-RAILS] 어떻게 레일 외부 루비 스크립트에서 액티브를 사용 하는가?
RUBY-ON-RAILS어떻게 레일 외부 루비 스크립트에서 액티브를 사용 하는가?
나는 쉽게 데이터베이스 모델에 액세스하는 액티브를 사용하고자하는 작은 루비 스크립트가 있습니다. 그것을 할 수있는 가장 좋은 방법은 무엇입니까?
해결법
-
==============================
1.
require 'active_record' # Change the following to reflect your database settings ActiveRecord::Base.establish_connection( adapter: 'mysql2', # or 'postgresql' or 'sqlite3' or 'oracle_enhanced' host: 'localhost', database: 'your_database', username: 'your_username', password: 'your_password' ) # Define your classes based on the database, as always class SomeClass < ActiveRecord::Base #blah, blah, blah end # Now do stuff with it puts SomeClass.find :all some_class = SomeClass.new
-
==============================
2.당신이 그것을 필요로 할 필요가 액티브 이후 버전 (V3 +)에 그 지적이의 가치가 너무 좋아
당신이 그것을 필요로 할 필요가 액티브 이후 버전 (V3 +)에 그 지적이의 가치가 너무 좋아
require "active_record"
-
==============================
3.당신은 단 몇 줄의 메모리 SQLite 데이터베이스와 최소한의 스크립트를 만들 수 있습니다. 이 답변은 요지로 사용할 수 있습니다.
당신은 단 몇 줄의 메모리 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.
from https://stackoverflow.com/questions/1643875/how-to-use-activerecord-in-a-ruby-script-outside-rails by cc-by-sa and MIT license
'RUBY-ON-RAILS' 카테고리의 다른 글
[RUBY-ON-RAILS] (4) 레일 - 부분적으로 가변 전달 (0) | 2020.02.18 |
---|---|
[RUBY-ON-RAILS] 다른 표준 "생산"이 아닌 데이터베이스 또는 "개발"에 레일 마이그레이션을 사용하여 (0) | 2020.02.18 |
[RUBY-ON-RAILS] 마이그레이션은 열 조합에 고유 제한 조건을 추가하는 (0) | 2020.02.18 |
[RUBY-ON-RAILS] 루비 온 레일스와 함께 사용하기 위해 창에 포스트 그레스 설치 (0) | 2020.02.18 |
[RUBY-ON-RAILS] 레일에 루비에 여러 데이터베이스에 연결 (0) | 2020.02.18 |