Home > Net >  Updating if exist or create if not rails
Updating if exist or create if not rails

Time:04-06

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

CodePudding user response:

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

CodePudding user response:

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

Explanation of what this is doing:

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

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 locked: false, if the model was not found and needs to be initialized, it sets locked to false 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.locked = false
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 locked to false and save the model.

A one-liner alternative would be

Model.where(column_name: value).first_or_create.update_attributes(locked: false)

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

  • Related