Update an attribute of the Active Record using a variable

VPaskar

I have an ActiveRecord named Station. I want to update some attributes not directly, but using a variable. Something like:

station = Station.where(attr1: "..." , attr2: "...") station.update_attributes(attr1: "...", attr2: "...", ..)

But when I initialize the station variable I get ActiveRecord::Relation, which doesn't have the update_attributes helper. How can i then update some fields of my Station using a variable, but not directly ?

Ilya

You should use ActiveRecord#find_by for this goal:

station = Station.find_by(attr1: "..." , attr2: "...") # Station first instance with specified attributes
station.update_attributes(attr1: "...", attr2: "...", ..)

This method returns Station instance if it exists with your attributes or nil in other case.

If you want find all Station instances and update each, you should use ActiveRecord#update_all method:

stations = Station.where(attr1: "..." , attr2: "...") #ActiveRecord::Relation
stations.update_all(attr1: "new_attr1" , attr2: "new_attr_2")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Write attribute to active record object

From Dev

Ruby on rails active record: update a field by another field in the same record using update_all

From Dev

Ruby on rails active record: update a field by another field in the same record using update_all

From Dev

Delete using active record

From Dev

How to update an active record in ruby

From Dev

active record update column id

From Dev

Find hash attribute value on active record rails

From Dev

Rails Active Record where object has an Attribute

From Dev

Value for Active Record attribute not getting saved properly

From Dev

Add Virtual attribute to active record object to output

From Dev

Add Virtual attribute to active record object to output

From Dev

Attr_reader for Active Record model attribute

From Dev

dynamic attribute based finders in active record 3.2.10

From Dev

Using a method for scopes in Active Record

From Dev

Using a method for scopes in Active Record

From Dev

How do I update a postgres boolean field using Active Record in Rails 5?

From Dev

Active Record - Find and Update (only if object exists)

From Dev

RoR active record restrict display expand update

From Dev

How to write active record using using scope

From Dev

Define variable to to be type of record attribute pl/sql

From Dev

Update JSON attribute after scope variable update

From Dev

Add Non Persistent attribute In Active Record JSON response

From Dev

Ruby on Rails / Active Record / MYSQL - Group by and order by a string attribute

From Dev

Using "BIGINT UNSIGNED" in Rails schema / Active Record

From Dev

Using find() in Active Record with multiple where clause

From Dev

How to define this association properly using active record?

From Dev

Using Descriptive Statistics gem on active record

From Dev

Advanced Active Record order by using regex

From Dev

tricky sql query using CI active record

Related Related

  1. 1

    Write attribute to active record object

  2. 2

    Ruby on rails active record: update a field by another field in the same record using update_all

  3. 3

    Ruby on rails active record: update a field by another field in the same record using update_all

  4. 4

    Delete using active record

  5. 5

    How to update an active record in ruby

  6. 6

    active record update column id

  7. 7

    Find hash attribute value on active record rails

  8. 8

    Rails Active Record where object has an Attribute

  9. 9

    Value for Active Record attribute not getting saved properly

  10. 10

    Add Virtual attribute to active record object to output

  11. 11

    Add Virtual attribute to active record object to output

  12. 12

    Attr_reader for Active Record model attribute

  13. 13

    dynamic attribute based finders in active record 3.2.10

  14. 14

    Using a method for scopes in Active Record

  15. 15

    Using a method for scopes in Active Record

  16. 16

    How do I update a postgres boolean field using Active Record in Rails 5?

  17. 17

    Active Record - Find and Update (only if object exists)

  18. 18

    RoR active record restrict display expand update

  19. 19

    How to write active record using using scope

  20. 20

    Define variable to to be type of record attribute pl/sql

  21. 21

    Update JSON attribute after scope variable update

  22. 22

    Add Non Persistent attribute In Active Record JSON response

  23. 23

    Ruby on Rails / Active Record / MYSQL - Group by and order by a string attribute

  24. 24

    Using "BIGINT UNSIGNED" in Rails schema / Active Record

  25. 25

    Using find() in Active Record with multiple where clause

  26. 26

    How to define this association properly using active record?

  27. 27

    Using Descriptive Statistics gem on active record

  28. 28

    Advanced Active Record order by using regex

  29. 29

    tricky sql query using CI active record

HotTag

Archive