tags:

views:

82

answers:

2

Hi guys, i have an action called individual user pdf per page, it communicates with two model, the user model which retrives information from the database, but specific information only for all users, it then takes the information and send it to the model report which, then when i inspect the attributes of each user it is displayed in way that i have not called it from the model user, it takes each pair and places it in a different order, so when i write information to a table on a pdf it displays it bad, like initials might be the last record of my table and cellphone might be the first one.. and i have to create a new page for each and every user's information..

my report code look's like:

def self.generate_individual_pdf(people,residents,user,estate = nil, title = nil)

pdf = PDF::Writer.new
title = !title ? "Report" :title
pdf.select_font "Times-Roman"
pdf.text "<em>Compiled By: #{user.presentation_name}</em> <em>Estate: #{estate.name}</em>                                       <em>Created On: #{Time.now.strftime('%d/%m/%Y %H:%M')}</em>", :justification => :center
   x = 0
25.times do
  pdf.text ""+"                        "
  x+=1
  puts x
end
pdf.text "<em>User Report For: #{estate.name}</em> ", :font_size => 18, :bold => true, :justification => :center
if estate.logo
  pdf.image "#{RAILS_ROOT}/public"+estate.logo.public_filename,:width => 100, :height => 100, :resize => 1.1, :justification => :center
end
y = 0
#loop people to create table for every person
 people.each do |p| 
  pdf.start_new_page 
  pdf.text"                   "+p.Title.to_s+"  "+p.Firstname.to_s+"  "+p.Lastname.to_s, :font_size => 12, :justification => :center
  pdf.text ""+"                        "
  pdf.text ""+"                        "
  pdf.text "Please verify if your details below are correct, and rectify them in the third column of the table.", :font_size => 8
  pdf.text ""+"                        "
  table = PDF::SimpleTable.new
  table.column_order.push(*["head1","head2","head3"])
  headings = ["head1","head2","head3"]
  headings.each do |h|
    table.columns[h] = PDF::SimpleTable::Column.new(h)
    table.columns[h].heading = h.humanize
  end
  #display tables
   table.show_headings = false
   table.orientation = :right
   table.position = :left
   data = []
    p.attributes.each_pair do |h|
     data << { "head1" => h[0], "head2" => h[1], "head3" => "                                "}
   end


   table.data.replace data
   table.render_on(pdf)

     i = 0
   28.times do
    pdf.text ""+"                        "
    i+=1
    puts i 
  end
  pdf.text "Kind Regards"+ "                   "+p.Title.to_s+"  "+p.Firstname.to_s+"  "+p.Lastname.to_s, :font_size => 11
  pdf.text ""+"                        "
  pdf.text "Signature                          "+"......................................."

end

then it produces the output when it loops throught the p.attribute.each_pair do |h| data << {"head1" => h[0], "head2" => h[1], "head3" => " this should be nil "} end

please guys i have been trying to use all this kind of sorting but i just cant get the right answer..anyone who can help please feel free.. thanks in advance.

A: 

Hashes in ruby are inherently unordered objects. You aren't guaranteed to get data out of them in the same order you put it in.

It sounds like the pairs of attributes are coming out in a random order (or at least not the order you want). One possible way of dealing with this is...

p.attributes.keys.sort do |key|
  data << { "head1" => key, "head2" => p.attributes[key], "head3" => nil }
end

This will give you back all your attributes sorted alphabetically by value name

_Kevin
FYI, as of Ruby 1.9, hashes preserve their order: http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l86It's not relevant to this case, necessarily, but it's good to know.
Telemachus
It is still giving me problems it gives me the error: undefined method `>' for #<Array:0xb64b31f4>
Donald
A: 

Ruby 1.9 will contain ordered hashes however if you are with 1.8 then take a look at the dictionary class from ruby facets. ( http://facets.rubyforge.org/doc/api/more/classes/Dictionary.html )

This helped me do this in the past.

Lee Irving