I'm currently working on a Phoenix project where I'm unsatisfied with the way I'm calling fields in the templates.
The schema is currently
defmodule MyApp.Car do
use MyApp.Web, :model
schema "car" do
field :columnName, :string
end
end
car = Repo.get!(Car, id)
I'd like to be able to call the result with
car.column_name
rather than
car.columnName
Migrating the database isn't currently an option due to a number of applications using the database.
There is a source
option for field
.
:source - Defines the name that is to be used in database for this field.
defmodule MyApp.Car do
use MyApp.Web, :model
schema "car" do
field :column_name, :string, source: :columnName
end
end
I believe this could be done Ecto's field source mapper.
defmodule MyApp.Car do
use Ecto.Schema
@field_source_mapper fn
:column_name -> :columnName
x -> x
end
schema "car" do
field :column_name, :string
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With