# 如何使用 rails runner 和 /var/www/discourse 中的 Gemfile

**URL:** <https://meta.discourse.org/t/how-do-i-use-rails-runner-with-the-gemfile-in-var-www-discourse/285034>\
**Category:** Development\
**Created:** [2023年十一月10日 03:29 UTC](https://meta.discourse.org/t/how-do-i-use-rails-runner-with-the-gemfile-in-var-www-discourse/285034 "2023-11-10T03:29:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Dawson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chris_dawson/32/184486_2.png) [@Chris\_Dawson](https://meta.discourse.org/u/Chris_Dawson)\
**Post date:** [2023年十一月10日 03:29 UTC](https://meta.discourse.org/t/how-do-i-use-rails-runner-with-the-gemfile-in-var-www-discourse/285034/1 "2023-11-10T03:29:00Z")

</div>

我有一个脚本需要从 sqlite3 加载一些数据，然后将其导入到 discourse 中。

## rails runner

我假设我可以在 `/shared/add_in_reverse.rb` 中使用类似以下的代码：

```plaintext
require "sqlite3"

# 打开数据库
db = SQLite3::Database.new "sqlite3.db"

k = 1234
db.execute( "SELECT * from foo where id = ?'", [k] ) do |row|
   puts row
end

title = "hello there"
u = User.find_or_create_by(username: "mexample") do |user|
   user.email = "me@example.com"
end
c = Category.find_or_create_by(name: "Something by mexample") do |category|
   category.user = u
end
Topic.create!( title: title, category: c, user: u )

```

然后使用以下命令运行它：

`rails r /shared/my_custom_script.rb`

但是，这会失败并出现 **`cannot load such file -- sqlite3 (LoadError)`**

```plaintext
bundler: failed to load command: script/rails (script/rails)

/usr/local/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb:38:in `require': cannot load such file -- sqlite3 (LoadError)
        from /usr/local/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb:38:in `require'
        from /var/www/discourse/vendor/bundle/ruby/3.2.0/gems/bootsnap-1.17.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require'
        from /var/www/discourse/vendor/bundle/ruby/3.2.0/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:38:in `require'

```

## Gemfile 和 bundle exec？

我记得 Ruby 有时需要使用 Gemfiles？所以我想，如果我在 `/var/www/discourse` 目录下，我可以加上 `bundle exec` 前缀。但这会失败：

```plaintext
# bundle exec rails r /shared/add_in_reverse.rb
fatal: detected dubious ownership in repository at '/var/www/discourse'
To add an exception for this directory, call:

        git config --global --add safe.directory /var/www/discourse
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:81:in `rescue in new_client': We could not find your database: discourse. Which can be found in the database configuration file located at config/database.yml. (ActiveRecord::NoDatabaseError)

To resolve this issue:

- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.
...

```

## IMPORT=1

好的。我看到了一些带有 `IMPORT=1` 的示例。我需要它吗？

```plaintext
IMPORT=1 rails r /shared/add_in_reverse.rb
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.

If this is a development machine, remove the /var/www/discourse/Gemfile freeze
by running `bundle config unset deployment`.

The list of sources changed
The dependencies in your gemfile changed

You have added to the Gemfile:
* mysql2
* redcarpet
* sqlite3 (~> 1.3, >= 1.3.13)
* ruby-bbcode-to-md
* reverse_markdown
* tiny_tds
* csv
* parallel
# IMPORT=1 bundle exec rails r /shared/add_in_reverse.rb
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.

If this is a development machine, remove the /var/www/discourse/Gemfile freeze
by running `bundle config unset deployment`.

The list of sources changed
The dependencies in your gemfile changed

You have added to the Gemfile:
* mysql2
* redcarpet
* sqlite3 (~> 1.3, >= 1.3.13)
* ruby-bbcode-to-md
* reverse_markdown
* tiny_tds
* csv
* parallel

```

**我根本没有修改 Gemfile** ；我只想使用 Gemfile 中指定的 sqlite。

## 为什么有两个 rails/ruby？

问题是路径中的那个是错误的，我应该使用供应商提供的 Ruby 吗？我看到很多 Ruby 和 Rails。但是使用供应商提供的 Ruby 不起作用（或者我不知道如何正确使用它）。

```plaintext
# which ruby
/usr/local/bin/ruby
# which rails
/usr/local/bin/rails
# find /var/www/discourse/ | grep ruby | grep vendor | grep bin | grep rails
/var/www/discourse/vendor/bundle/ruby/3.2.0/bin/rails

# /var/www/discourse/vendor/bundle/ruby/3.2.0/bin/rails runner /shared/add_in_reverse.rb
/usr/local/lib/ruby/site_ruby/3.2.0/rubygems.rb:264:in `find_spec_for_exe': can't find gem railties (>= 0.a) with executable rails (Gem::GemNotFoundException)
        from /usr/local/lib/ruby/site_ruby/3.2.0/rubygems.rb:283:in `activate_bin_path'
        from /var/www/discourse/vendor/bundle/ruby/3.2.0/bin/rails:25:in `<main>'

```

---

<div class="post-metadata">

**Author:** ![Chris\_Dawson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chris_dawson/32/184486_2.png) [@Chris\_Dawson](https://meta.discourse.org/u/Chris_Dawson)\
**Post date:** [2023年十一月10日 20:25 UTC](https://meta.discourse.org/t/how-do-i-use-rails-runner-with-the-gemfile-in-var-www-discourse/285034/2 "2023-11-10T20:25:56Z")

</div>

我猜我可以用反引号和 `sqlite` 二进制文件？毕竟我只得到一行。但是，我不明白为什么我尝试做的事情并不容易，我认为 `sqlite` 是所有新 Rails 项目的默认数据库。它是否被 Discourse 故意禁用了？
