I understand how to run a simple piece of code in rails console. say
Swimming::Student.create(:name="Jerry")
How do I run a big piece of code (many lines)
Swimming::Student.all.each{ |student|
student.attended = flase
student.save
}
Just hit enter, as you'd expect:
$rails c
Loading development environment (Rails 3.2.13)
2.0.0p0 :001 > Student.all.each do |student| #enter
2.0.0p0 :002 > puts student #enter
2.0.0p0 :003?> end #enter
# here comes the output
Related
Something unusual is happening with my app. I have a model called User and I made one instance from the Rails console. I can get it from the console, but not from the app.
Any hint on what may cause this behaviour?
If I run User.count in console I get 1, in app I get 0.
Running Rails.configuration.database_configuration[Rails.env] gives the same result in console and app: {"adapter"=>"sqlite3", "pool"=>5, "timeout"=>5000, "database"=>"db/development.sqlite3"}
(Running Ruby 2.6.0 and Rails 5.2.3)
I'm starting to think that Rails is making a fool of me 😹
Edit. My controller method:
def index
puts "DB CONF: #{Rails.configuration.database_configuration[Rails.env]}"
puts "USER COUNT: #{User.count}" #prints 0 to console
render plain: "#{User.count}" #prints 0 on page
end
Code in console:
Loading development environment (Rails 5.2.3)
2.6.0 :001 > User.count
(0.5ms) SELECT COUNT(*) FROM "users"
=> 1
This question already has an answer here:
Rails table_name_prefix is not working as expected
(1 answer)
Closed 1 year ago.
I have this code:
# app/models/ta.rb
module Ta
def self.table_name_prefix
'ta_'
end
end
...
# app/models/ta/article.rb
module Ta
class Article < ActiveRecord::Base
end
end
From the rails console...
# development environment
Loading development environment (Rails 4.1.6)
2.1.3 :001 > Ta::Article.table_name
=> "ta_articles"
2.1.3 :002 >
...
# production environment
Loading production environment (Rails 4.1.6)
2.1.3 :001 > Ta::Article.table_name
=> "articles"
2.1.3 :002 >
Why is this happening?
Add to config/initializers/namespace.rb something like:
require Rails.root.join('app', 'models', 'ta')
should solve your problem.
Since about last week, my usually working rails project started behaving badly in regards to BigDecimal.to_f. Every BigDecimal number gets output as 0.0 when calling to_f:
~/development/rails_project> rails c
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > BigDecimal.new('5').to_f
=> 0.0
I'm running Ruby 1.9.3 patch level 194, and Rails 3.2.9, as I always have. Some co-workers who also work on the same project using the same versions don't have this issue.
Also tried with Ruby 1.9.3 patch level 362 with the same results. Has anyone had the same problem and knows how to overcome it?
Thanks.
EDIT:
My problem is not with the to_s function, but the to_f. I probably over-simplified, because my issue is with number_to_currency:
~/development/rails_project> rails c
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > include ActionView::Helpers::NumberHelper
=> Object
1.9.3p194 :002 > BigDecimal.new('5').to_s('F')
=> "5.0"
1.9.3p194 :003 > BigDecimal.new('5').to_s
=> "5.0"
1.9.3p194 :004 > BigDecimal.new('5').to_f
=> 0.0
1.9.3p194 :005 > number_to_currency(5)
=> "€ 0,00"
With some debugging, I narrowed it down to the number_with_precision method, more precisely to the line:
rounded_number = BigDecimal.new(number.to_s).round(precision).to_f
If I output the different parts of this line, I get the following results:
number = 5.0
number.to_s = "5.0"
precision = 2
BigDecimal.new(number.to_s) = 5.0
BigDecimal.new(number.to_s).round(precision) = 5.0
BigDecimal.new(number.to_s).round(precision).to_f = 0.0
Any clue?
Just a shot in the dark: This might be a locale-dependant formatting issue of the Ruby/Rails console.
Try this:
BigDecimal.new('5').to_s('F') # Should be "5.0"
BigDecimal.new('5').to_i # Should be 5
If this ouputs the expected result, then it is just a formatting issue of the console. The to_f method actually returns the correct result, but the console calls another to_s on the result in order to print it. In that case you should be able to reproduce the problem with 5.to_f, too.
In Java I might do:
public static void doSomething();
And then I can access the method statically without making an instance:
className.doSomething();
How can I do that in Ruby? this is my class and from my understanding self. makes the method static:
class Ask
def self.make_permalink(phrase)
phrase.strip.downcase.gsub! /\ +/, '-'
end
end
But when i try to call:
Ask.make_permalink("make a slug out of this line")
I get:
undefined method `make_permalink' for Ask:Class
Why is that if i haven't declared the method to be private?
Your given example is working very well
class Ask
def self.make_permalink(phrase)
phrase.strip.downcase.gsub! /\ +/, '-'
end
end
Ask.make_permalink("make a slug out of this line")
I tried in 1.8.7 and also in 1.9.3
Do you have a typo in you original script?
All the best
There is one more syntax which is has the benefit that you can add more static methods
class TestClass
# all methods in this block are static
class << self
def first_method
# body omitted
end
def second_method_etc
# body omitted
end
end
# more typing because of the self. but much clear that the method is static
def self.first_method
# body omitted
end
def self.second_method_etc
# body omitted
end
end
Here's my copy/paste of your code into IRB. Seems to work fine.
$ irb
1.8.7 :001 > class Ask
1.8.7 :002?>
1.8.7 :003 > def self.make_permalink(phrase)
1.8.7 :004?> phrase.strip.downcase.gsub! /\ +/, '-'
1.8.7 :005?> end
1.8.7 :006?>
1.8.7 :007 > end
=> nil
1.8.7 :008 > Ask.make_permalink("make a slug out of this line")
=> "make-a-slug-out-of-this-line"
Seems to work. Test it out in your irb as well, and see what results you're getting. I'm using 1.8.7 in this example, but I also tried it in a Ruby 1.9.3 session and it worked identically.
Are you using MRI as your Ruby implementation (not that I think that should make a difference in this case)?
In irb make a call to Ask.public_methods and make sure your method name is in the list. For example:
1.8.7 :008 > Ask.public_methods
=> [:make_permalink, :allocate, :new, :superclass, :freeze, :===,
...etc, etc.]
Since you also marked this as a ruby-on-rails question, if you want to troubleshoot the actual model in your app, you can of course use the rails console: (bundle exec rails c) and verify the publicness of the method in question.
I am using ruby 1.9.3 and the program is running smoothly in my irb as well.
1.9.3-p286 :001 > class Ask
1.9.3-p286 :002?> def self.make_permalink(phrase)
1.9.3-p286 :003?> phrase.strip.downcase.gsub! /\ +/, '-'
1.9.3-p286 :004?> end
1.9.3-p286 :005?> end
=> nil
1.9.3-p286 :006 > Ask.make_permalink("make a slug out of this line")
=> "make-a-slug-out-of-this-line"
It's also working in my test script. Nothing wrong with your given code.It's fine.
I would think this is a Ruby difference, but I'm using the same Ruby version 1.8.7. This is related to this post (to answer "why do you need this?"). This code works in 2.2.2
Loading development environment (Rails 2.2.2)
>> module ActionMailer
>> Utils.normalize_new_lines("blah")
>> end
but in 2.3.5 it fails
Loading development environment (Rails 2.3.5)
>> module ActionMailer
>> Utils.normalize_new_lines("blah")
>> end
NoMethodError: undefined method `normalize_new_lines' for ActionMailer::Utils:Module
from (irb):2
What's new about 2.3.5 that this would fail? The method is there in 2.3.5, so this works
Loading development environment (Rails 2.3.5)
>> include ActionMailer
>> include Utils
>> normalize_new_lines("blah")
I realize this is probably an important Rails difference.
Looks like the code changed from version 2.2 to version 2.3.5
old:
module ActionMailer
module Utils #:nodoc:
def normalize_new_lines(text)
text.to_s.gsub(/\r\n?/, "\n")
end
module_function :normalize_new_lines
end
end
new:
module ActionMailer
module Utils #:nodoc:
def normalize_new_lines(text)
text.to_s.gsub(/\r\n?/, "\n")
end
end
end
I guess you could restore the old behavior by calling module_function yourself:
$ script/console
Loading development environment (Rails 2.3.5)
>> module ActionMailer
>> module Utils
>> module_function :normalize_new_lines
>> end
>> Utils.normalize_new_lines("blah")
>> end
=> "blah"
>>
EDIT: Or better yet just include the module (per Simone)
>> include ActionMailer::Utils