Save only the year in a date field in Postgres! RAILS

0

I have two fields called start_anno and termino_an, where I want to save only the year that the user will select and save to the database. But when you save the form that is processed by RAILS, it saves year, month, and day.

IwouldliketoknowifIcansaveadatetypedatainPostgresbystoringonlytheyearthroughRAILS.

IntheWebpageandbasicallyasyougetthedata

Thatinhtml.erbthecodeislikethis[]

In the database there is a table that would be the creation of user by devise.

Andinthisphotobelowarethefieldsthatareaddedtotheusertable,inwhichfollowsthefieldsstart_anandtermino_ano

    
asked by anonymous 20.11.2018 / 03:21

1 answer

1

In this case, you will have to change the field type in the database in your migration.

from - (instead of using datetime)

create_table :nome_da_tabela do |t|
  t.datetime :inicio_ano
  t.datetime :termino_ano
end

To - (to use an integer)

create_table :nome_da_tabela do |t|
  t.integer :inicio_ano
  t.integer :termino_ano
end

And at the time of saving you must extract the year. Example: Time.year returns 2018

    
20.11.2018 / 03:47