I use Ruby 1.9.3 and Rails 3.2.9, when I do the following in a rails console:
1.9.3p125 :003 > "foot".pluralize
=> "foots" # shouldn't it be "feet"?
1.9.3p125 :004 > "tooth".pluralize
=> "tooths" # shouldn't it be "teeth"?
1.9.3p125 :009 > "goose".pluralize
=> "gooses" # shouldn't it be "geese"?
is that a bug in rails pluralize or I did something wrong?
You can configure the rails inflector. There should be an initializer file in your application to do so: config/initializers/inflections.rb
You can then add a call to "teach" rails the new rule:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'tooth', 'teeth'
end
After you restart the server/console the new pluralization should be in place.
Related
I'm new in Rails and now I'm trying to make a custom pluralization so I put this in my initializers/inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.clear
# Irregulares
inflect.irregular "país", "países"
end
But when I try on rails console I get:
Running via Spring preloader in process 3137
Loading development environment (Rails 5.0.2)
2.3.3 :001 > "país".pluralize
=> "país"
2.3.3 :002 >
I also tried to put this in my inflections.rb but with the same result:
ActiveSupport::Inflector.inflections ("pt-BR") do |inflect|
inflect.clear
# Irregulares
inflect.irregular "país", "países"
end
As I'm using rails-i18n my application.rb has this extra line:
config.i18n.default_locale = 'pt-BR'
I searched a lot and didn't find a solution.
Any suggestions?
Try running the console without Spring with DISABLE_SPRING=1
Out of curiosity, where is Rails.application defined? If I do this in irb in a Rails-powered app, I got an error:
$ irb
1.9.3-p327 :001 > require 'rails/application'
=> true
1.9.3-p327 :002 > class App < Rails::Application; end
NoMethodError: undefined method `application' for Rails:Module
I have tried requiring more, like active_support and rails/railtie/configuration but no luck.
The purpose of this is to load a minimal Rails env where I can test an ActiveRecord::Base helper :)
Usually when working with Rails you use rails console instead of IRB. When you run rails console it will boot up your Rails application.
FWIW, Rails.application is defined in railties:
lib/rails.rb
i have some problem with removing diacritics with iconv in ruby on rails
here is my code:
class Diacritics
def removeDiacritics(text)
dRemover = Iconv.new("ASCII//TRANSLIT", "UTF-8")
text = dRemover.iconv(text).gsub(/[^a-zA-Z0-9 ]/, '')
end
end
this is output:
1.9.3-p392 :001 > require "diacritics"
/usr/local/rvm/gems/ruby-1.9.3-p392#persoc/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
=> true
1.9.3-p392 :002 > remover = Diacritics.new
=> #<Diacritics:0x00000004237068>
1.9.3-p392 :003 > text = "Dánský prezídent"
=> "Dánský prezídent"
1.9.3-p392 :004 > remover.removeDiacritics(text)
=> "Dnsk prezdent"
i expect "Dansky prezident"
server apache on fedora (httpd), using rvm and ruby 1.9.3-p392
Can anybody help me?
You can use the ActiveSupport::Inflector.transliterate method.
ActiveSupport::Inflector.transliterate("Dánsky prezídent") # => "Dansky prezident"
If you need this for a url slug, it's even easier.
"Dánsky prezídent".parameterize # => "dansky-prezident"
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'm trying to generate a model called ClassAttendance, but Rails keeps naming the migrations class_attendances. I've tried correcting this problem by placing the following code the following code in \config\initializers\inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable "attendance"
end
This seems to work fine in the rails console:
$ rails console
Loading development environment (Rails 3.2.6)
irb(main):001:0> "attendance".pluralize
=> "attendance"
Unfortunately, the rails model generator seems to be unaffected:
$ rails generate model ClassAttendance
invoke active_record
create db/migrate/20120806201910_create_class_attendances.rb
create app/models/class_attendance.rb
invoke rspec
create spec/models/class_attendance_spec.rb
Does it have something to do with this?
irb(main):002:0> "class_attendance".pluralize
=> "class_attendances"
Or is there some other problem I'm not seeing?
That is the workaround, you need to place it in the inflections.rb file in the config/initializers/. So your config/initializers/inflections.rb would be
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w( attendance class_attendance ClassAttendance)
end