rails mass update column by inserting value calculated from other columns

YOHAN

I am making a sports leaderboard in rails and trying to mass assign a column with values calculated from other columns. Following is the names of the columns: :pts, :games_played, and :pts_per_game. The first two already have values assigned and I want to insert value into the last one by dividing :pts by :games_played. So I went to rails console and tried

PlayerLeaderboard.update_all(pts_per_game: pts/games_played)

But I get this output with NoMethodError

NoMethodError: undefined method `pts' for main:Object
    from (irb):24
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /Users/AYL/Dropbox/workspace/rails/krossover/bin/rails:8:in `<top (required)>'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'irb(main):025:0>

The weird thing is I tried this approach before and it worked. I just switched my development db from sqlite3 to postgres and re-seeded data. It seems very unlikely to me that switching db is the reason for this error; I must have missed something.
I also tried update_column.

Thanks in advance!

mu is too short

That code is treating pts and games_played as local variables or methods on self. There are no such methods, hence the error.

You want columns so you need to send a bit of SQL into update_all instead of a Ruby hash:

PlayerLeaderboard.update_all('pts_per_game = pts / games_played')

Presumably pts and games_played are integer columns so you're going to have truncation problems here. If that's what you want (i.e. 5 / 3 being 1) then there's nothing to worry about. If your pts_per_game is floating point, then you'll need to throw a cast in the mix:

PlayerLeaderboard.update_all('pts_per_game = cast(pts as float) / games_played')
PlayerLeaderboard.update_all('pts_per_game = pts::float / games_played')

The cast is standard SQL, the :: syntax is PostgreSQL-specific.

If your pts_per_game is an integer and you want it rounded, then you'll have to say so:

PlayerLeaderboard.update_all('pts_per_game = round(cast(pts as float) / games_played)')

If negative values are possible then you'll need to use ceil or floor instead of round to choose which direction your negative values should be rounded.

If you have zeros in games_played then you'll need to do two updates and decide what pts_per_game should be when there haven't been any games, zero and NULL would be reasonable choices I suppose:

PlayerLeaderboard.where(:games_played => 0).update_all(...)
PlayerLeaderboard.where.not(:games_played => 0).update_all(:pts_per_game => 0)

If you're going with NULL for pts_per_game when there aren't any games then you only need the first update_all since new columns are NULL by default.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update a column with a calculated value

From Dev

Create Pandas column with the max of two calculated values from other columns

From Dev

Update a column with information from 3 other columns

From Dev

Update column with value from two potential columns

From Dev

SharePoint 2013 Calculated Column- Evaluate 3 other columns and return non-null value

From Java

inserting a column of categories based on other columns in R

From Dev

Update a Value in One Column Based on Criteria in Other Columns

From Dev

TSQL View with Calculated Column from View Columns

From Dev

Update table value when inserting to other table

From Dev

Select max value from column for every value in other two columns

From Dev

Pandas update one df from another based on other columns value

From Dev

Inserting a Column from One Sheet to the Other Sheet

From Dev

Update value of other column on update of this column

From Dev

pandas: add new column with value from either of two other columns

From Dev

get latest column value from hive table conditionally on other columns

From Dev

Unable to update database column value from rails console

From Dev

Unable to update database column value from rails console

From Dev

How can I update a column with PL\SQL by using a calculated value

From Dev

Inserting an optional value into a db column from sum

From Dev

column with value as Concatenate other columns' value

From Dev

Update table column based on columns in other table

From Dev

SQL Update a column dependent on other two columns

From Dev

Update a column based on the value of other column

From Dev

SQL : get substring from one column and inserting it into other column

From Dev

MySQL Update - Set value in Table A with a value calculated from Table B

From Dev

Calculated column with the sum of values from many columns in a row

From Dev

How do I find the maximum value of a column and the other information of the other columns from the same raw in R?

From Dev

SQL Server 2000 - Returning a single column value from a 4 column table based on the values of the other 3 columns

From Dev

SQL mass replace column value

Related Related

  1. 1

    Update a column with a calculated value

  2. 2

    Create Pandas column with the max of two calculated values from other columns

  3. 3

    Update a column with information from 3 other columns

  4. 4

    Update column with value from two potential columns

  5. 5

    SharePoint 2013 Calculated Column- Evaluate 3 other columns and return non-null value

  6. 6

    inserting a column of categories based on other columns in R

  7. 7

    Update a Value in One Column Based on Criteria in Other Columns

  8. 8

    TSQL View with Calculated Column from View Columns

  9. 9

    Update table value when inserting to other table

  10. 10

    Select max value from column for every value in other two columns

  11. 11

    Pandas update one df from another based on other columns value

  12. 12

    Inserting a Column from One Sheet to the Other Sheet

  13. 13

    Update value of other column on update of this column

  14. 14

    pandas: add new column with value from either of two other columns

  15. 15

    get latest column value from hive table conditionally on other columns

  16. 16

    Unable to update database column value from rails console

  17. 17

    Unable to update database column value from rails console

  18. 18

    How can I update a column with PL\SQL by using a calculated value

  19. 19

    Inserting an optional value into a db column from sum

  20. 20

    column with value as Concatenate other columns' value

  21. 21

    Update table column based on columns in other table

  22. 22

    SQL Update a column dependent on other two columns

  23. 23

    Update a column based on the value of other column

  24. 24

    SQL : get substring from one column and inserting it into other column

  25. 25

    MySQL Update - Set value in Table A with a value calculated from Table B

  26. 26

    Calculated column with the sum of values from many columns in a row

  27. 27

    How do I find the maximum value of a column and the other information of the other columns from the same raw in R?

  28. 28

    SQL Server 2000 - Returning a single column value from a 4 column table based on the values of the other 3 columns

  29. 29

    SQL mass replace column value

HotTag

Archive