Linux command
rails-console 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Start an interactive console
rails console
Use the short alias
rails c
Start in sandbox mode
rails console --sandbox
Start console in a specific environment
rails console -e [production|test|development]
Sandbox in a specific environment
rails console -e staging --sandbox
说明
rails console opens an interactive Ruby (IRB) session with the full Rails application environment loaded. This includes all models, configuration, and database connections, making it a powerful tool for exploring and debugging applications. The console provides direct access to ActiveRecord models for querying and manipulating data, testing methods and business logic, and inspecting application state. It inherits the full context of the specified Rails environment. Sandbox mode wraps the entire session in a database transaction that rolls back when you exit. This allows safe experimentation with data without permanent changes, useful for testing destructive operations or debugging production issues.
参数
- -e, --environment _ENV_
- Specify the Rails environment (development, test, production). Defaults to development.
- --sandbox, -s
- Rollback any database changes made during the session on exit.
- --skip-executor, -w
- Do not wrap the console with the Rails Executor (skips query cache, reloading, and callbacks).
- -h, --help
- Show help information.
FAQ
What is the rails-console command used for?
rails console opens an interactive Ruby (IRB) session with the full Rails application environment loaded. This includes all models, configuration, and database connections, making it a powerful tool for exploring and debugging applications. The console provides direct access to ActiveRecord models for querying and manipulating data, testing methods and business logic, and inspecting application state. It inherits the full context of the specified Rails environment. Sandbox mode wraps the entire session in a database transaction that rolls back when you exit. This allows safe experimentation with data without permanent changes, useful for testing destructive operations or debugging production issues.
How do I run a basic rails-console example?
Run `rails console` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -e, --environment _ENV_ do in rails-console?
Specify the Rails environment (development, test, production). Defaults to development.