# Where are my field values?!

**URL:** <https://www.ruby-forum.com/t/where-are-my-field-values/195433>\
**Category:** Rails\
**Created:** [September 23, 2010, 7:52am UTC](https://www.ruby-forum.com/t/where-are-my-field-values/195433 "2010-09-23T07:52:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 23, 2010, 7:52am UTC](https://www.ruby-forum.com/t/where-are-my-field-values/195433/1 "2010-09-23T07:52:19Z")

</div>

I have created a function in model that retrieves the idea of creating  
it uisng this link:

> <https://stackoverflow.com/questions/1330273/how-to-calculate-difference-of-two-fields-on-form-submit-ruby-on-rails>

The aim of the function is to calculate the following formula, provided  
that all are fields in the table:

quantity\_sold = receive + deliver

This is how my model looks like:

Class Iteminfo \< ActiveRecord::Base  
attr\_accessor :receive, :deliver  
before\_create :calculate\_quantity\_sold

private  
def calculate\_quantity\_sold  
self.quantity\_sold = receive.to\_i + deliver.to\_i  
end  
end

Now, when I enter values for the “deliver” and “receive” fields, say:

receive = 10  
deliver = 10

I get the following value for “quantity\_sold” —\> 20

Where is the problem?

The problem is that when I enter the values on the form for the  
“receive” and “deliver” fields, and then hit the “Create” buttom, in the  
“show” page, I get a “20” beside the “quantity\_sold” field shown and  
EMPTY fields for “deliver” and “receive”.

The other problem is that when I for example want to see the data in my  
table using Firefoxe’s SQLite Manager I do NOT get any data shown  
although I can see it in the form “index” page.

What is the problem here?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 23, 2010, 9:33am UTC](https://www.ruby-forum.com/t/where-are-my-field-values/195433/2 "2010-09-23T09:33:33Z")

</div>

Regarding the fields in SQL Manager, I found that I was browsing the  
wrong database.

And, in my model in order to get values for “receive” amd “deliver” I  
had to remove this line:

attr\_accessor :receive, :deliver

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 23, 2010, 11:48am UTC](https://www.ruby-forum.com/t/where-are-my-field-values/195433/3 "2010-09-23T11:48:38Z")

</div>

The function should bbe

Def quantity\_sold  
Delivered\_item + item\_price  
End

Then call quantity\_sold.

Removing attr\_accesible is a very bat idea.

2010/9/23, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> –  
> You received this message because you are subscribed to the Google G.  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid.  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Enviado desde mi dispositivo móvil

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 23, 2010, 12:01pm UTC](https://www.ruby-forum.com/t/where-are-my-field-values/195433/4 "2010-09-23T12:01:32Z")

</div>

On 23 September 2010 10:47, radhames brito [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
topposted again without any sign of trimming:

> The function should bbe
> 
> Def quantity\_sold  
> Delivered\_item + item\_price  
> End

If you’re going to write example code, please take the time to make  
sure it might even work… you should be aware of the case-sensitivity  
of Ruby.  
What on earth does “item\_price” have to do with quantity sold? Where  
has the OP mentioned that? And infact, where has “Delivered\_item” come  
from? Can I just check that you replied to the right message?

> Then call quantity\_sold.

The OP has stated he wants to store the quantity\_sold in the DB - how  
will this method help achieve that?

> Removing attr\_accesible is a very bat idea.

He hasn’t said anything about removing attr\_accesible - he’s removed  
the attr\_accessor definitions - which is correct if he’s got fields  
for those values he doesn’t need to create accessors for them.

The question might be to check with the OP whether he _really_ needs  
to store the calculated value, or whether it would just be easier to  
have a method (similar to the one you proposed, but using the correct  
calculation) to return the value when asked. I suppose it depends  
whether he’s doing a lot of DB queries on the quantity\_sold - in which  
case it might be better to have it in the DB, of if it’s just for use  
in views, then possible a (memoized) calculated value might be  
“better” to prevent any risk of the values getting out-of sync  
(nothing worse than someone running an update on the db and changing  
values without updating the calculated values…)

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 23, 2010, 12:39pm UTC](https://www.ruby-forum.com/t/where-are-my-field-values/195433/5 "2010-09-23T12:39:35Z")

</div>

ok now im awake and on my pc , yes my reply made no sense, the problem  
is  
that i cant trimm comments from the phone and i cant see the op as i  
reply,  
and since i had just woke up , i couldnt quite remember what the op was  
about as i reply.

I wont repeat that.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 23, 2010, 12:07pm UTC](https://www.ruby-forum.com/t/where-are-my-field-values/195433/6 "2010-09-23T12:07:58Z")

</div>

Sorry im replying from i new phone and is 5:00 am where i live , i  
should have waited until i get to my pc and drink some coffe

2010/9/23, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> sure it might even work… you should be aware of the case-sensitivity
> 
> > Removing attr\_accesible is a very bat idea.  
> > case it might be better to have it in the DB, of if it’s just for use  
> > removed\_email\_address@domain.invalid.  
> > For more options, visit this group at  
> > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Enviado desde mi dispositivo móvil

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 23, 2010, 5:20pm UTC](https://www.ruby-forum.com/t/where-are-my-field-values/195433/7 "2010-09-23T17:20:31Z")

</div>

Thanks a lot everyone.
