Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
How can I get Rails 5 to play nicely with a primary key that has a period in it?

I'm working with a database I have no control over, and cannot make alterations to. This database has a table called warehouse_items. Each warehouse item is uniquely identified by a primary key indicating the item id.

Unfortunately, that primary key attribute is named WAREHOUSE_ITEM.ID

(Note the obnoxious period between "item" and "id")


When I try to run a basic query, such as:

WarehouseItem.find('wh3453')

I get an Undefined Table error.

Fortunately, when looking at what Rails is attempting to do, the problem becomes obvious:

: SELECT  "warehouse_items".* FROM "warehouse_items" WHERE "WAREHOUSE_ITEM"."ID" = $1 LIMIT $2

Because of the period in the attribute name, Rails is treating "WAREHOUSE_ITEM.ID" as a table/attribute combination, rather than an attribute name with a period in it.

When I run the following PSQL query by hand, I get exactly what I need:

SELECT  "warehouse_items".* FROM "warehouse_items" WHERE "warehouse_items"."WAREHOUSE_ITEM.ID" = 'wh3453'

Why is Rails screwing this up, and how can I fix it?


EDIT:

Also worth noting: I've tried using self.primary_key to override the primary key to no avail.

I've tried both a string and a symbol, as in:

self.primary_key="WAREHOUSE_ITEM.ID"

and

self.primary_key=:"WAREHOUSE_ITEM.ID"

Neither one has worked...

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Thanks for all the help, everyone!

A suggestion in the comments to use find_by_sql does work! However, I stumbled onto a different solution that works even better.

First, I aliased the annoying attribute name to something simple: id

alias_attribute :id, :"WAREHOUSE_ITEM.ID"

Notice that it's still a symbol, which is important for the next step.

I then overwrite the primary_key method with a custom function:

  def self.primary_key
    return "id"
  end

Now, when I do WarehouseItem.find('wh3453'), Rails defaults to checking id, which is aliased to the correct symbol and it works as intended!!!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda