Development Tip

레일 명명 규칙을 어떻게 재정의합니까?

yourdevel 2020. 10. 23. 19:12
반응형

레일 명명 규칙을 어떻게 재정의합니까?


저는 "의류"라는 이름의 모델을 가지고 있는데, 그 모델은 제가 하나의 옷이되고 싶습니다. 기본적으로 rails는 복수형이 옷이라고 말합니다. 옳든 그르 든, 복수형이 "clothes"이면 더 읽기 쉬울 것이라고 생각합니다.

복수 명명 규칙을 어떻게 재정의합니까? 반복해서 할 필요가 없도록 모델에서 바로 할 수 있습니까? 이로 인해 경로가 처리되는 방식이 어떻게 변경됩니까 (편안한 아키텍처를 사용하고 있습니다)?


저는 RoR 전문가는 아니지만 가능한 접근 방식을 찾았습니다 . 참조 된 사이트에서 config/initializers/inflections.rb파일 내부에 굴절 규칙을 추가 할 수 있습니다 .

# Add new inflection rules using the following format 
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'clothing', 'clothes'
end

레일 2.3.2 및 2+의 경우 약간 다른 작업이 필요합니다.

ActiveSupport::Inflector.inflections do |inflect|
    inflect.plural /^(ox)$/i, '\1\2en'
    inflect.singular /^(ox)en/i, '\1'

    inflect.irregular 'octopus', 'octopi'

    inflect.uncountable "equipment"
end

environment.rb데이터베이스 복수화를 중지하려는 경우 파일에 추가하십시오.

ActiveRecord::Base.pluralize_table_names = false

Ruby 2.2.2 Windows 또는 Linux를 사용하면 가장 좋은 해결책은 다음과 같습니다.

ActiveRecord::Base.pluralize_table_names = false

class Persona < ActiveRecord::Base
end


personas = Persona.all
personas.each do | personita |
  print "#{personita.idpersona}   #{personita.nombre}\n"
end

p Persona.count

참고 URL : https://stackoverflow.com/questions/1185035/how-do-i-override-rails-naming-conventions

반응형