Hello, I haven''t used class variables before (I''m new to RoR and OOP in general...) and I think I need to use them for part of my program. I have several different views for my controller "compare". For each view, I need to grab the number of rows corresponding to several particular SQL statements. I was thinking I could do something like this: class CompareController < ApplicationController @number_of_rows = Table.connection.select_all(" SELECT COUNT(*) AS n FROM table") @@numofrows = @number_of_rows.to_s.slice(1,3).to_i def index etc... Then use @@numofrows in the view like so: <%= link_to "entries (#{@@numofrows})", :controller => ''compare'', :action => ''index'' %> Obviously, this doesn''t work. I am new to this type of programming and I am trying to read up on all of this, but I am still very confused on how to do this type of thing... Any help is appreciated! Thank you, - Jeff Miller -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nevermind, I dumped the class variable idea and now am using globals instead, since they don''t need to be initialized. Thanks, - Jeff -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi, I''m not sure I know enough about what you want to do, but I suspect that neither class variables nor globals are a good solution. They almost never are. If you want the count of rows in some table named "Table," then you can obtain it with Table.count. You could store this in an instance variable, which will be available in your views. For example, in your controller, def some_action @row_count = Table.count end And in your view, <%= link_to "entries (#{@row_count})", :controller => ''compare'', :action => ''index'' %> If you find yourself fetching the row count in several actions, you could investigate using a before_filter. Good luck, Craig --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hello, I advice you to use your model when you run any operation for your database. Here is my code, i hope it can help you. ---------------- CONTROLLER ---------------- class CompareController < ApplicationController def index ... end def count_row @number_of_rows = Table.lets_count_row @slicing = @number_of_rows.to_s.slice(1,3).to_i end #if you want execute it in any pages of your view in this controller #you can do like it def my_page count_row end ... end ---------------- YOUR TABLE MODEL ---------------- class Table < ActiveRecord::Base def lets_count_row self.count end OR def lets_count_row self.count_by_sql("select count(*)from tables") end .. end ---------------- YOUR VIEW count_row.rhtml ---------------- <%= link_to "entries (#{@slicing})", :controller => ''compare'', :action => ''index'' %> If you want run def count_row to entire controller, you should put def count_row in application.rb, and you not need rewrite def count_row in any controller. Example: --------------- application.rb ---------------- class ApplicationController < ActionController::Base def count_row @number_of_rows = Table.lets_count_row @slicing = @number_of_rows.to_s.slice(1,3).to_i end end ----------------------- any_name_controller.rb ----------------------- class AnyName < ApplicationController before_filter :count_row ... end and the view is the same way and it could be used to any view from any controller. *~*~*~*~*~**~*~*~*~*~* Reinhart Ariando YM : Booking2Heaven WEB : http://teapoci.blogspot.com -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---