Hi guys,
I have this situation,
#migration data
class CreateStats < ActiveRecord::Migration
def self.up
create_table :stats do |t|
t.integer :points
t.integer :assists
t.integer :blocks
t.integer :turnovers
end
def self.down
drop_table :stats
end
end
#Model
class Stat < ActiveRecord:Base
end
How i can store integer data type and when application display the data back
(Since it will do calculation) it will display float on each column points,
assists, blocks and turnovers?
Thanks for your help
ys
--
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.
On 10 March 2010 07:49, Yudi Soesanto <soesanty-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi guys, > > I have this situation, > > #migration data > class CreateStats < ActiveRecord::Migration > def self.up > create_table :stats do |t| > t.integer :points > t.integer :assists > t.integer :blocks > t.integer :turnovers > end > > def self.down > drop_table :stats > end > end > > #Model > class Stat < ActiveRecord:Base > > end > > How i can store integer data type and when application display the data back > (Since it will do calculation) it will display float on each column points, > assists, blocks and turnovers?I think you need to give more detail of what you are attempting to achieve, give an example. Colin -- 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.
Yudi Soesanto wrote:> How i can store integer data type and when application display the data > back > (Since it will do calculation) it will display float on each column > points, > assists, blocks and turnovers?This seems to be a basic Ruby question. See the following IRB session:>> x = 25=> 25>> y = 3=> 3>> z = x / y=> 8>> z = x.to_f / y.to_f=> 8.33333333333333>> puts "%0.3f" % z8.333 -- 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.
Here is my situation,
In the database, I have a *stats* table which has:
- points (Integer datatype)
- assists (Integer datatype)
- blocks (Integer datatype)
- turnovers (Integer datatype)
In rails, I have *stat* model
Now If I want to display value from stats table, I can do this
@stats = Stat.find(:all)
It will display all integer value that user entered *(Note: User ONLY
allowed to enter integer value)*
For example, I have these data in stats table
*Points* *assists* *blocks* *turnovers*
1 4 5 1
5 2 5 5
7 3 2 7
Now, I want to average each column and assign it to stat class
@average_stat = Stat.new
stats = Stat.find(:all)
@average_stat.points = stats.sum(''points'') /
stats.count(''points'')
@average_stat.assists = stats.sum(''assists'') /
stats.count(''assists'')
@average_stat.assists = stats.sum(''blocks'') /
stats.count(''blocks'')
@average_stat.assists = stats.sum(''turnovers'') /
stats.count(''turnovers'')
The problem I see, when I assigning to average value to @average_stat, the
value get converted to integer. I want to have float value. How I can assign
float value in stat class?
Yudi Soesanto
On Wed, Mar 10, 2010 at 11:07 PM, Robert Walker
<lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:
> Yudi Soesanto wrote:
> > How i can store integer data type and when application display the
data
> > back
> > (Since it will do calculation) it will display float on each column
> > points,
> > assists, blocks and turnovers?
>
> This seems to be a basic Ruby question. See the following IRB session:
> >> x = 25
> => 25
> >> y = 3
> => 3
> >> z = x / y
> => 8
> >> z = x.to_f / y.to_f
> => 8.33333333333333
> >> puts "%0.3f" % z
> 8.333
> --
> 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
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.
If @average_stat is Stat.new then @average_stat.points,
@average_stat.assists, etc... are also integer. So you could use a
array variable, after stats = Stat.find(:all) :
@average_stat=[ stats.sum(''points'') /
stats.count(''points''),
stats.sum(''assists'') /
stats.count(''assists'') , stats.sum(''blocks'')
/
stats.count(''blocks'') ,
stats.sum(''turnovers'') /
stats.count(''turnovers'') ] .
In fact you shoud use directly average(stat.points), etc.. See
http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M002188
--
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.
I believe you have the solution in the previous answer. When you do this division:> @average_stat.points = stats.sum(''points'') / stats.count(''points'')you divide two integers and ruby gives you an integer. That''s the way it is. so instead do this division : (as Robert suggested) @average_stat.points = stats.sum(''points'').to_f / stats.count(''points'').to_f Then the next question is what''s gonna append if you try to save @average_stat as this object doesn''t carry integers anymore Hope this helps, Le 11 mars 2010 à 06:28, Yudi Soesanto a écrit :> Here is my situation, > > In the database, I have a stats table which has: > - points (Integer datatype) > - assists (Integer datatype) > - blocks (Integer datatype) > - turnovers (Integer datatype) > > In rails, I have stat model > > Now If I want to display value from stats table, I can do this > @stats = Stat.find(:all) > It will display all integer value that user entered (Note: User ONLY allowed to enter integer value) > > For example, I have these data in stats table > Points assists blocks turnovers > 1 4 5 1 > 5 2 5 5 > 7 3 2 7 > > Now, I want to average each column and assign it to stat class > > @average_stat = Stat.new > stats = Stat.find(:all) > > @average_stat.points = stats.sum(''points'') / stats.count(''points'') > @average_stat.assists = stats.sum(''assists'') / stats.count(''assists'') > @average_stat.assists = stats.sum(''blocks'') / stats.count(''blocks'') > @average_stat.assists = stats.sum(''turnovers'') / stats.count(''turnovers'') > > The problem I see, when I assigning to average value to @average_stat, the value get converted to integer. I want to have float value. How I can assign float value in stat class? > > > Yudi Soesanto > > > On Wed, Mar 10, 2010 at 11:07 PM, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > Yudi Soesanto wrote: > > How i can store integer data type and when application display the data > > back > > (Since it will do calculation) it will display float on each column > > points, > > assists, blocks and turnovers? > > This seems to be a basic Ruby question. See the following IRB session: > >> x = 25 > => 25 > >> y = 3 > => 3 > >> z = x / y > => 8 > >> z = x.to_f / y.to_f > => 8.33333333333333 > >> puts "%0.3f" % z > 8.333 > -- > 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@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > -- > 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.-- 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.
if you have integer type fields than how can you save float value in it. On Mar 11, 10:28 am, Yudi Soesanto <soesa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Here is my situation, > > In the database, I have a *stats* table which has: > - points (Integer datatype) > - assists (Integer datatype) > - blocks (Integer datatype) > - turnovers (Integer datatype) > > In rails, I have *stat* model > > Now If I want to display value from stats table, I can do this > @stats = Stat.find(:all) > It will display all integer value that user entered *(Note: User ONLY > allowed to enter integer value)* > > For example, I have these data in stats table > *Points* *assists* *blocks* *turnovers* > 1 4 5 1 > 5 2 5 5 > 7 3 2 7 > > Now, I want to average each column and assign it to stat class > > @average_stat = Stat.new > stats = Stat.find(:all) > > @average_stat.points = stats.sum(''points'') / stats.count(''points'') > @average_stat.assists = stats.sum(''assists'') / stats.count(''assists'') > @average_stat.assists = stats.sum(''blocks'') / stats.count(''blocks'') > @average_stat.assists = stats.sum(''turnovers'') / stats.count(''turnovers'') > > The problem I see, when I assigning to average value to @average_stat, the > value get converted to integer. I want to have float value. How I can assign > float value in stat class? > > Yudi Soesanto > > On Wed, Mar 10, 2010 at 11:07 PM, Robert Walker <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote: > > > Yudi Soesanto wrote: > > > How i can store integer data type and when application display the data > > > back > > > (Since it will do calculation) it will display float on each column > > > points, > > > assists, blocks and turnovers? > > > This seems to be a basic Ruby question. See the following IRB session: > > >> x = 25 > > => 25 > > >> y = 3 > > => 3 > > >> z = x / y > > => 8 > > >> z = x.to_f / y.to_f > > => 8.33333333333333 > > >> puts "%0.3f" % z > > 8.333 > > -- > > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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.
soesanty-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2010-Mar-12 22:06 UTC
Re: Re: Data Type Question
Thanks for your help guys. Yudi Sent from my AXIS Worry Free BlackBerry® smartphone -----Original Message----- From: RavionRails <ravindrasingh22-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> Date: Thu, 11 Mar 2010 02:15:10 To: Ruby on Rails: Talk<rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> Subject: [Rails] Re: Data Type Question if you have integer type fields than how can you save float value in it. On Mar 11, 10:28 am, Yudi Soesanto <soesa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Here is my situation, > > In the database, I have a *stats* table which has: > - points (Integer datatype) > - assists (Integer datatype) > - blocks (Integer datatype) > - turnovers (Integer datatype) > > In rails, I have *stat* model > > Now If I want to display value from stats table, I can do this > @stats = Stat.find(:all) > It will display all integer value that user entered *(Note: User ONLY > allowed to enter integer value)* > > For example, I have these data in stats table > *Points* *assists* *blocks* *turnovers* > 1 4 5 1 > 5 2 5 5 > 7 3 2 7 > > Now, I want to average each column and assign it to stat class > > @average_stat = Stat.new > stats = Stat.find(:all) > > @average_stat.points = stats.sum(''points'') / stats.count(''points'') > @average_stat.assists = stats.sum(''assists'') / stats.count(''assists'') > @average_stat.assists = stats.sum(''blocks'') / stats.count(''blocks'') > @average_stat.assists = stats.sum(''turnovers'') / stats.count(''turnovers'') > > The problem I see, when I assigning to average value to @average_stat, the > value get converted to integer. I want to have float value. How I can assign > float value in stat class? > > Yudi Soesanto > > On Wed, Mar 10, 2010 at 11:07 PM, Robert Walker <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote: > > > Yudi Soesanto wrote: > > > How i can store integer data type and when application display the data > > > back > > > (Since it will do calculation) it will display float on each column > > > points, > > > assists, blocks and turnovers? > > > This seems to be a basic Ruby question. See the following IRB session: > > >> x = 25 > > => 25 > > >> y = 3 > > => 3 > > >> z = x / y > > => 8 > > >> z = x.to_f / y.to_f > > => 8.33333333333333 > > >> puts "%0.3f" % z > > 8.333 > > -- > > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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. -- 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.