Data Type Question

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

On 10 March 2010 07:49, Yudi S. [email protected] wrote:

  t.integer :blocks

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

Yudi S. 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

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

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 S. a écrit :

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

Thanks for your help guys.

Yudi
Sent from my AXIS Worry Free BlackBerry® smartphone

if you have integer type fields than how can you save float value in
it.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs