使用 Rails Inflections 和 `rails generate` [英] Using Rails Inflections with `rails generate`

查看:41
本文介绍了使用 Rails Inflections 和 `rails generate`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个名为 ClassAttendance 的模型,但 Rails 一直将迁移命名为 class_attendances.我已经尝试通过将以下代码放在 \config\initializers\inflections.rb 中来纠正这个问题:

ActiveSupport::Inflector.inflections do |inflect|inflect.uncountable 出席"结尾

这似乎在 rails 控制台中工作正常:

$ rails 控制台加载开发环境(Rails 3.2.6)irb(main):001:0>出席".复数=>出席"

不幸的是,rails 模型生成器似乎没有受到影响:

$ rails 生成模型 ClassAttendance调用 active_record创建 db/migrate/20120806201910_create_class_attendances.rb创建应用程序/模型/class_attendance.rb调用 rspec创建规范/模型/class_attendance_spec.rb

跟这个有关系吗?

irb(main):002:0>"class_attendance".pluralize=>课堂出勤"

或者还有其他一些我没有看到的问题吗?

解决方案

这就是解决方法,你需要把它放在 config/initializers/inflections.rb 文件中/代码>.所以你的 config/initializers/inflections.rb 将是

<前>ActiveSupport::Inflector.inflections 做 |inflect|inflect.uncountable %w(出勤 class_attendance ClassAttendance)结尾

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

这篇关于使用 Rails Inflections 和 `rails generate`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆