rails-console

How to use let variables in rails console?

浪尽此生 提交于 2021-02-19 04:14:58
问题 using rspec 2.6.4 rails 3.1.6 How to use let variables in rails test console? 1.9.3-p0 :032 > let(:user) { create(:user) } NoMethodError: undefined method `let' for main:Object Please advise, which library should be required here? For example: below is executed in console to use stub methods in console. require 'rspec/mocks/standalone' Is it possible, to define and call let variables in rails console? 回答1: If you are fine with let just creating globals, you can polyfill it like this: def let

Enable stack dump on exception in Rails console

核能气质少年 提交于 2020-01-23 05:00:18
问题 I am using Ruby on Rails with the pry gem. When an error occurs during a rake task, I get a very nice stack trace. When however I do something in my rails console that triggers an exception, I only get to see the error message and the one line of code that triggered it (which most of the time is somewhere in the rails core). Is there a way to enable these stack dumps in the console? 回答1: I found a solution myself. Apparently, I was in need of the command wtf? that comes with pry. [7] project

rails c not working in rails 5

馋奶兔 提交于 2020-01-01 01:48:09
问题 On using the command in terminal inside a rails 5 application rails c the error thrown is given bellow. I have no idea what this means in a similar question here that for which the solution was to use spring stop. I have tried that too but no it still gave the same error. it would be great if somebody can point out the mistake here. Running via Spring preloader in process 6457 /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'

How can I run an ActiveJob in Rails console for debugging?

天涯浪子 提交于 2019-12-23 08:49:39
问题 I currently have an ActiveJob that I've created and use Sidekiq to queue it. I'm wanting to debug the job, but for me to see any messages I program into it I have to check my log files. I feel like it would be more convenient to be able to see my puts messages in my job in the Rails Console. When I run the perform_later method though in rails console it just queues the job up and I never see the messages in console. Is there a way to make it where I will see them in the console? 回答1: You can

Truncate table(s) with rails console

大兔子大兔子 提交于 2019-12-20 10:17:08
问题 I have this testingdatabase which, by now, is stuffed with junk. Now I've done a few Table.destroy_all commands in the rails console which deletes all records and dependencies which is awesome. However; I'd like to truncate everything so the ID's etc. start at 1 again. Is there any way in Rails 3? 回答1: The accepted answer only works if you need to recreate the whole database. To drop a single table (with the callbacks) and to get the IDs to start from 1: Model.destroy_all # Only necessary if

How to create a custom method for the rails console?

南笙酒味 提交于 2019-12-19 04:02:01
问题 When I'm using the Rails console in Ubuntu for a long session I define the clear method: def clear; system 'clear' end So when my console is getting dirty the only thing I have to do is type clear and the console gets cleared. I would like to use this function without to re-type it every time. Thanks in advance. 回答1: Just put it in the ~/.irbrc file. It gets loaded every time you run irb or rails console . The Rails console is just irb with your Rails application environment loaded. Find more

NameError: uninitialized constant (rails)

荒凉一梦 提交于 2019-12-18 11:07:10
问题 I have a simple model called PhoneNumber: class PhoneNumber < ActiveRecord::Base validates :pnumber, presence: true, on: :create #=> { :message => " cannot be blank" } validates :pnumber, numericality: true, on: :create end I go to the root folder of the application (the one containing the app sub-folder) and start the console: rails console --sandbox When I try to create an empty PhoneNumber (I want to get an error message as the validation shall fail) I am getting the following error

Rails Console find users by array of ids

天涯浪子 提交于 2019-12-18 10:47:06
问题 So I have an array of user ids. Is there a way in rails console to query all of these user's with the array something like ids = [1, 2, 3, 4] users = User.find(ids) and have it return all 4 users? 回答1: For an array, you can use one of these: # Will raise exception if any value not found User.find( [1,3,5] ) # Will not raise an exception User.find_all_by_id( [1,3,5] ) # Rails 3 User.where(id: [1,3,5]) # Rails 4 If you happen to be using a range, you can use these: # Will raise exception if any

Rails console 'y' helper returns NameError rather than yaml-formatting output

本小妞迷上赌 提交于 2019-12-17 22:54:07
问题 I'm trying to use y object in Rails 3.2.6/Ruby 1.9.3 console to get nicely formatted yaml output for an ActiveRecord object, but for some reason it isn't working for me. I've used it in the past, but somewhere along the way it broke. I get the following output when I try: NameError: undefined local variable or method `yaml' for main:Object 回答1: The y method is actually an extension to the Kernel object put in place by the Syck YAML parser/emitter. Here are the last few lines of lib/ruby/1.9.1