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? Thanks. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Ali <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>:> 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 > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Enviado desde mi dispositivo móvil -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 23 September 2010 10:47, radhames brito <rbritom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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...) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> On 23 September 2010 10:47, radhames brito <rbritom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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...) > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Enviado desde mi dispositivo móvil -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks a lot everyone. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.