rails-console

Rails Console - Find where created at = certain day

落爺英雄遲暮 提交于 2019-12-02 20:21:10
With Ruby on Rails console, is it possible to query the database for all records created on a certain day? something like date = "january 5 2013" users = User.find(:all, :conditions => {:created_at => date}) You can do it like this: date = Date.parse('january 5 2013') users = User.where(created_at: date.midnight..date.end_of_day) Dheer Yes, It is possible like: date = Date.parse("january 5 2013") users = User.where(created_at: date) but created_at is type of date-time like 2014-01-28 08:35:00.9608 and I think All user have different created_at So you may used like this User.where("created_at =

Write to rails console

风流意气都作罢 提交于 2019-12-02 18:42:24
When I want to try or debug smthing I run rails console and do some stuff there. I can print some text or variables from code by raising exception with raise "blablabla" . Question: How I can just write to rails console without exception raising (and obvious breaking code execution) like a simple logger.info "blah" ? Nobita As other have said, you want to use either puts or p . Why? Is that magic? Actually not. A rails console is, under the hood, an IRB , so all you can do in IRB you will be able to do in a rails console. Since for printing in an IRB we use puts , we use the same command for

Can I get the Ruby on Rails console to remember my command history, umm, better?

北战南征 提交于 2019-12-02 16:55:36
I'm using the console in Ruby on Rails 3.1.1, and I'm finding its command history (up arrow) to be really flaky. I've run the commands p = Product.by_name 'Acme' and p.add_search_term('Snipe') several times today, across several console sessions. Yet, when I reload the Ruby on Rails console, only the first one shows in my command history, not the second. Sometimes they are both there in the history after I reload the console. On top of that, I see commands in my history that are from several days ago after pressing up arrow only a few times. Is there some sort of configuration that I need to

Executing a command every time the rails console starts

我怕爱的太早我们不能终老 提交于 2019-12-02 02:25:24
问题 I have a setup command that I want executed every time I start the rails console - MyClass.some_method() I get tired of retyping it each time I fire up rails c - is there a way to have it automatically get run every time a new console is started? Thanks! 回答1: We do this in order to ask for the tenant every time the console starts. It took a bit of investigation, but we got it working fairly elegantly. Note that this works with Rails 5.2 but it has worked mostly the same way since Rails 4.

Executing a command every time the rails console starts

二次信任 提交于 2019-12-02 01:39:06
I have a setup command that I want executed every time I start the rails console - MyClass.some_method() I get tired of retyping it each time I fire up rails c - is there a way to have it automatically get run every time a new console is started? Thanks! We do this in order to ask for the tenant every time the console starts. It took a bit of investigation, but we got it working fairly elegantly. Note that this works with Rails 5.2 but it has worked mostly the same way since Rails 4. Another thing to note is that this is written specifically because we wanted to be able to run the method once

How to create a custom method for the rails console?

六月ゝ 毕业季﹏ 提交于 2019-11-30 23:44:51
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. 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 infos about irb here: http://ruby-doc.com/docs/ProgrammingRuby/html/irb.html#S2 Put this function to ~/

set rails console stack backtrace limit permanently

无人久伴 提交于 2019-11-30 09:05:59
问题 rails console by default boots with context.back_trace_limit=16 , which can be changed to whatever you want simply by typing context.back_trace_limit=n . The problem is you have to type it each time you boot rails c . Where do I change the context.back_trace_limit permanently? Some more reading on rails console configuration appreciated. 回答1: You have to create/edit your ~/.irbrc with the following: IRB.conf[:BACK_TRACE_LIMIT]= 20 To be taken into account: The options must be uppercased This

Rails console not working on server

一个人想着一个人 提交于 2019-11-30 05:58:25
When I run bundle exec rails console production or rails console production via SSH on the server in the Current folder of the Capistrano deploy I get: Usage: rails new APP_PATH [options] Options: (...) with an explanation to start a new app. Locally it works. Why can't I start a console remotely? I'm assuming that you updated to rails 4 from version 3 and your app can't find the executables in the bin directory. Run this to see your rails version: $ rails -v If your rails version is 4 or above, try running this: $ rake rails:update:bin Source: Rails 4 Release Notes 6.1 Notable changes Your

NameError: uninitialized constant (rails)

寵の児 提交于 2019-11-30 02:54:53
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 message: 2.0.0-p451 :001 > PhoneNumber.new NameError: uninitialized constant PhoneNumber from (irb):1 from

Rails Console find users by array of ids

久未见 提交于 2019-11-29 22:50:37
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? 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 value not found User.find((1..4).to_a) #same as User.find([1,2,3,4]) # Will not raise an exception User