# Comment utiliser rails runner avec le Gemfile dans /var/www/discourse

**URL:** <https://meta.discourse.org/t/how-do-i-use-rails-runner-with-the-gemfile-in-var-www-discourse/285034>\
**Category:** Development\
**Created:** [Novembre 10, 2023, 3: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:** 1\
**Showing post:** 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:** [Novembre 10, 2023, 3: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>

J’ai un script qui doit charger des données à partir d’une base de données sqlite3, puis les importer dans Discourse.

## rails runner

J’ai supposé que je pouvais utiliser quelque chose comme ceci dans `/shared/add_in_reverse.rb` :

```plaintext
require "sqlite3"

# Ouvrir une base de données
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 )

```

Et, ensuite l’exécuter avec :

`rails r /shared/my_custom_script.rb`

Mais, cela échoue avec **`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 et bundle exec ?

Je me suis rappelé que ruby avait parfois besoin d’utiliser des Gemfiles ? Alors, j’ai pensé que si j’étais dans le répertoire `/var/www/discourse`, je pourrais préfixer `bundle exec`. Mais, cela échoue :

```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

Ok. J’ai vu quelques exemples avec `IMPORT=1`. En ai-je besoin ?

```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

```

**Je n’ai pas du tout modifié le Gemfile** ; je veux juste utiliser le sqlite spécifié dans ce Gemfile.

## Pourquoi deux rails/ruby ?

Le problème est-il que celui dans le chemin est incorrect, et que je devrais utiliser le ruby vendu ? Je vois un tas de rubies et de rails. Mais utiliser celui vendu ne fonctionne pas (ou je ne sais pas comment l’utiliser correctement).

```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>'

```

---

_[View the full topic](https://meta.discourse.org/t/how-do-i-use-rails-runner-with-the-gemfile-in-var-www-discourse/285034)._
