# Unsure of how to update values using a constant

**URL:** <https://www.ruby-forum.com/t/unsure-of-how-to-update-values-using-a-constant/174951>\
**Category:** Rails\
**Created:** [September 4, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/unsure-of-how-to-update-values-using-a-constant/174951 "2009-09-04T15:25:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 4, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/unsure-of-how-to-update-values-using-a-constant/174951/1 "2009-09-04T15:25:39Z")

</div>

Here is my code:

> <https://gist.github.com/elricstorm/180875>

Please note that I’ve outlined the issue in the link but will add a bit  
more information here in terms of processing:

The method is a universal method for approx. 37 models. This means that  
37 models will use this method to create and update data that is being  
parsed from [ncaa.org](http://ncaa.org).

There are 37 constants which contain all of the fields representing that  
particular model’s table.

When no data is found, the values for those constants are created for  
each model’s current week. The issue I’m having is when trying to use  
the same constant to update data on top of the current week if data  
already exists. I thought I would be able to use save or save! but  
those methods won’t exist with the current way I’m processing the data.

If anyone can help me with the last part of the method after (else)  
where I’m trying to update the data, I’d appreciate it.

I’ve added notes to the link to help.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 4, 2009, 3:42pm UTC](https://www.ruby-forum.com/t/unsure-of-how-to-update-values-using-a-constant/174951/2 "2009-09-04T15:42:22Z")

</div>

Why won’t model.update\_attributes(values) work?

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 4, 2009, 3:45pm UTC](https://www.ruby-forum.com/t/unsure-of-how-to-update-values-using-a-constant/174951/3 "2009-09-04T15:45:26Z")

</div>

jhaagmans wrote:

> Why won’t model.update\_attributes(values) work?

It will give the following error:

undefined method `update\_attributes’ for #Class:0x902444c

values is a hash

As an example, The first field in the table is rank, so values[field] is  
values[:rank] OR

{:compiled\_on=\>“2009-09-04”, :rank=\>“1”}:Hash

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 4, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/unsure-of-how-to-update-values-using-a-constant/174951/4 "2009-09-04T16:17:31Z")

</div>

Thanks for the explanation Fred. With create I didn’t have to specify  
the row ids to update because it was simply creating them. I’ll find a  
way to locate the row to update and pass the id and hash,… thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 4, 2009, 4:36pm UTC](https://www.ruby-forum.com/t/unsure-of-how-to-update-values-using-a-constant/174951/5 "2009-09-04T16:36:21Z")

</div>

As a quick and dirty test,

puts “Updating Current Data in #{model} for the following teams:”  
x = 0  
y = model.compiled\_this\_week.find(:all)  
parser\_object.rows.each do |row|  
team = Team.find\_by\_name(row[1])  
values = {:compiled\_on =\> Date.today.strftime(’%Y-%m-%d’)}  
constant.each\_with\_index do |field, i|  
if row[i] == row[1]  
values[field] = team.id  
else  
values[field] = row[i]  
end  
end  
# List each team we update  
puts team.name + " ID = " + team.id.to\_s  
model.update(y[x], values)  
x += 1  
end

This portion of the update process works fine using this. However, I  
had to call model find again and iterate over the ids in no particular  
order. I don’t mind iterating over the ids without a precise order  
because the data being overwritten is being overwritten for the current  
week against 120 rows of data. So, it doesn’t matter if they are in a  
different order, so long as the data for each row is exact, which by  
this quick and dirty test is.

I’ll have to find a cleaner way of implementing it though.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 4, 2009, 4:08pm UTC](https://www.ruby-forum.com/t/unsure-of-how-to-update-values-using-a-constant/174951/6 "2009-09-04T16:08:43Z")

</div>

On Sep 4, 2:25 pm, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> When no data is found, the values for those constants are created for  
> each model’s current week. The issue I’m having is when trying to use  
> the same constant to update data on top of the current week if data  
> already exists. I thought I would be able to use save or save! but  
> those methods won’t exist with the current way I’m processing the data.
> 
> If anyone can help me with the last part of the method after (else)  
> where I’m trying to update the data, I’d appreciate it.

update expects either an id and a hash of attributes, or a hash of ids  
to attributes, ie

update(1, {:name =\> ‘bob’})

update the row with id 1 and sets the name to bob.

the other form assumes you are passing an array of ids, and an array  
of hashes, so

update([1,2], [{:name =\> ‘bob’}, {:name =\> ‘alice’}])

update the row with id 1 and sets the name to bob and updates the row  
with id 2 and sets the name to alice

Fred
