Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

884
Views
Updating if exist or create if not rails

So im using an api to get info on weather, its executes everyday, what im trying to do is to get updated if already exist, and create a new one if it doesn't in table. I do want to update all attributs when udpdating. i did try

model = Model.where(column_name: value).first_or_initialize(locked: false)

but i get an error saying :

unknown attribute locked for Model

raise UnknownAttributeError.new(self ,k.to_s)

If you need anything, ask and i will comment or edit. Im newb to ruby and rails

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Firstly refer to the docs here A table by the name of weather with the following attributes location: string temperature:integer wind:string needing to be updated or initialized based on the location would work like this

 @weather_record = Weather.find_or_initialize_by(location: location_value)
 @weather.temperature = -60
 @weather.wind = strong
 @weather.save

Next, never, ever use a reserved name for a model so do not have Model as the name of your table

Lastly in your example

model.Model.where(column_name: value).first_or_initialize(locked: false)

you are saying a_record.ClassName.where which is just wrong, If you are using a class method then start with the class name e.g. Weather.where if you are using instance methods then use the instance name e.g. an_instance_of_weather.some_field

over 4 years ago · Santiago Trujillo Report

0

Firstly, the model.Model part should be just Model, as Model is your class.

locked is supposed to be a column/attribute of the Model class, although it seems is not the case judging from your error. Therefore, I'm gonna use other_column_name as an example.

Explanation of what this is doing:

Model.where(column_name: value).first_or_initialize(other_column_name: some_value)

Model.where(column_name: value): gets models that satisfy the condition column_name == value

first_or_initialize: if a model such that column_name == value was found, that one is returned. Otherwise, it initializes a model with column_name = value.

By passing other_column_name: some_value, if the model was not found and needs to be initialized, it sets other_column_name to some_value but: 1) it does not update it if it was initially found and 2) it does not save the record.

The equivalent of first_or_initialize that saves the new record would be first_or_create but this would still not update the record if it already existed.

So, you should do something like this:

m = Model.where(column_name: value).first_or_initialize
m.other_column_name = some_value
m.save

This way, you first get a model where column_name is value or initialize a new one with this value if it didn't already exist. Then, you set the attribute other_column_name to some_value and save the model.

A one-liner alternative would be

Model.where(column_name: value).first_or_create.update(other_column_name: some_value)

However, note that if it needs to be created, this one will perform 2 queries (the insert and the update).

About the error part. It says the attribute locked does not exist on the Model record. Are these classes you created? Are you using some pre-existing project? You could try posting Model.attribute_names and maybe your schema.rb

over 4 years ago · Santiago Trujillo Report

0

Try this mate:

column_name_value = (Way that you get the info from data)
model = Model.find_or_initialize_by column_name: column_name_value

Let me know if worked!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!