Where are my field values?!

I have created a function in model that retrieves the idea of creating it uisng this link: http://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?


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

The function should bbe

Def quantity_sold Delivered_item + item_price End

Then call quantity_sold.

Removing attr_accesible is a very bat idea.

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...)

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

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.

Thanks a lot everyone.