I''m still playing with my first Rails application and learning a lot. Now that I''ve had some more time to play, I have lingering questions I could use some pointers for. Forgive the rambling nature of this message, it''s mostly a brain dump of thoughts I''ve had while taking my first steps with Rails. 1. Is there an easy way to tell if my production server is using FastCGI? I deployed up to TextDrive today to play around a little, and it feels quite sluggish. I want to be sure it''s running the way I thought it would be. 2. I have some pages where I let you add a bunch of objects. Multiple phone numbers, for example. There''s a method in the controller that let''s you add home, office, fax, mobile and other numbers. I''m setting that up with code like this, in the controller: @phone1 = Phone.new @phone2 = Phone.new @phone3 = Phone.new @phone4 = Phone.new @phone5 = Phone.new Needless to say, the views and the create method in the controller are worse. I know I''m being dumb here, so I need a few pointers. Obviously, I want to use an Array. Is that possible? Will I end up with html like <label for="phones[2]_...">? Is that okay? What about my @params when they get to the create method? Originally, I intended to just render the partial form in a loop to make the view easy, but I couldn''t figure out how to use this to get the data back to the create method correctly. I ended up making a multi_form partial. Is that the best I can do? Guide me to a better solution and I''ll be grateful. 3. In my applications, constructing a contact is an involved process and can touch many database tables. I currently walk the user through it page by page, but I can see power users preferring a single entry form. That would be a LOT of objects, many of them interrelated in some way. Have people built forms like this with Rails? Do I just throw them all in the page and sort it all out when I get them back in the controller''s create method? Any other tips along these lines? 4. Does ActiveRecord provide any tools for "ORDER BY" statments? Say I want to find_all(), but with a certain order. Is the correct approach to use find_by_sql() for that? 5. What is "require_dependancy"? I see it used, but can''t seem to find documentation on it. (Are the Web site API docs current?) 6. When do I use helpers? Are they documented somewhere I''m missing? Are they for adding methods to views? What if I want a method in a controller that you can''t browse to? Now if I want to add something to my ActiveRecord subclass, I can just do that directly, right? For example, say the database stores the name in pieces and I want to assemble it in a full_name() field. I can just def full_name() ... end and implement it in terms of the others, right? Okay, that should keep me busy for a while. Thanks for taking the time to read through all my newbie problems! James Edward Gray II
James Edward Gray II wrote:> 4. Does ActiveRecord provide any tools for "ORDER BY" statments? Say I > want to find_all(), but with a certain order. Is the correct approach > to use find_by_sql() for that?find_all, find_first, and the dynamic finders can take an ordering argument, such as: Contact.find_all(nil, ''name ASC'') Payment.find_all_by_amount(amount, ''created_on'') As well, associations can have default orderings. has_many :comments, :order => "posted_on" -- Lee
Treat all my remarks below as wrong until confirmed by someone smart. On Apr 7, 2005 3:40 PM, James Edward Gray II <james-AUi9nNu29NfWNcQ1/nO7itHuzzzSOjJt@public.gmane.org> wrote:> I''m still playing with my first Rails application and learning a lot. > Now that I''ve had some more time to play, I have lingering questions I > could use some pointers for. > > Forgive the rambling nature of this message, it''s mostly a brain dump > of thoughts I''ve had while taking my first steps with Rails. > > 1. Is there an easy way to tell if my production server is using > FastCGI? I deployed up to TextDrive today to play around a little, and > it feels quite sluggish. I want to be sure it''s running the way I > thought it would be.You should be using lighttpd and fastcgi on TextDrive. File a support ticket.> > 2. I have some pages where I let you add a bunch of objects. Multiple > phone numbers, for example. There''s a method in the controller that > let''s you add home, office, fax, mobile and other numbers. I''m setting > that up with code like this, in the controller: > > @phone1 = Phone.new > @phone2 = Phone.new > @phone3 = Phone.new > @phone4 = Phone.new > @phone5 = Phone.new > > Needless to say, the views and the create method in the controller are > worse. > > I know I''m being dumb here, so I need a few pointers. Obviously, I > want to use an Array. Is that possible? Will I end up with html like > <label for="phones[2]_...">? Is that okay? What about my @params when > they get to the create method? > > Originally, I intended to just render the partial form in a loop to > make the view easy, but I couldn''t figure out how to use this to get > the data back to the create method correctly. I ended up making a > multi_form partial. Is that the best I can do? > > Guide me to a better solution and I''ll be grateful.This is probably non-optimal, but I (think I) do something like view: <% 5. times do |i| %> <input name=phone[<%= i %>] > # or use whatever html you like <% end %> controller: 5.times do |i| if @params[''phone''][i].nil != "" Phone.new # whatever end end> > 3. In my applications, constructing a contact is an involved process > and can touch many database tables. I currently walk the user through > it page by page, but I can see power users preferring a single entry > form. That would be a LOT of objects, many of them interrelated in > some way. Have people built forms like this with Rails? Do I just > throw them all in the page and sort it all out when I get them back in > the controller''s create method? Any other tips along these lines? > > 4. Does ActiveRecord provide any tools for "ORDER BY" statments? Say > I want to find_all(), but with a certain order. Is the correct > approach to use find_by_sql() for that?I think you can use: find_by_sql ("order by name")> > 5. What is "require_dependancy"? I see it used, but can''t seem to > find documentation on it. (Are the Web site API docs current?)It''s like ''require'' except cooler. (no clue)> > 6. When do I use helpers? Are they documented somewhere I''m missing? > Are they for adding methods to views? What if I want a method in a > controller that you can''t browse to?They''re used for functions that you want in your views (or controllers). For example, this morning I needed to display prices. Prices in my app are stored as a float. I added a function into helpers/applictation_helper.rb (that''s a ''global'' helper that all views have access to) that added a to_money() function to Float. So then I''d call item.price.to_money and it returned the formatted price. (Except that I couldn''t get the function added to float, so I had to add a plain old method to the helper (I used money()) so I had to use in my view: money(item.price). )> > Now if I want to add something to my ActiveRecord subclass, I can just > do that directly, right? For example, say the database stores the name > in pieces and I want to assemble it in a full_name() field. I can just > def full_name() ... end and implement it in terms of the others, right?I think so. You ask good questions.
On Thursday 07 April 2005 18:40, James Edward Gray II wrote:> 4. Does ActiveRecord provide any tools for "ORDER BY" statments? Say > I want to find_all(), but with a certain order. Is the correct > approach to use find_by_sql() for that?Take a look at the api docs for find_all: find_all(conditions = nil, orderings = nil, limit = nil, joins = nil) Example: User.find_all(nil, ''last_login DESC'') Up-to-date docs are available here: http://rails.rubyonrails.com/ Or, you can use gem_server.> 5. What is "require_dependancy"? I see it used, but can''t seem to > find documentation on it. (Are the Web site API docs current?)Seems like the documentation ought to be updated... In any case, require_dependency is used to reload source files on each request when in development mode. This is a great feature -- changes you make to your code will take effect on the next request. (restarting the webserver isn''t required.)> 6. When do I use helpers? Are they documented somewhere I''m missing? > Are they for adding methods to views? What if I want a method in a > controller that you can''t browse to?Helpers are usually used to simply view code and remain dry. If you seem to be repeating yourself in your templates (or anywhere, really ;) you should probably add a helper method. A common situation is a link for a model. def book_link(book, options = {}) x = {:title => book.title}.update(options) return link_to("Read #{h book.title}", x) end If you want to add a non-action method to your controller, just make it protected or private. If you want it to be public, there is some way to do this. Perhaps it is hidden_methods :method_name> Now if I want to add something to my ActiveRecord subclass, I can just > do that directly, right? For example, say the database stores the name > in pieces and I want to assemble it in a full_name() field. I can just > def full_name() ... end and implement it in terms of the others, right?For sure. You can also override fields from the db: class Article ... def by_line read_attribute(''by_line'') || "by #{self.author.name}" end end -- Nicholas Seckar aka. Ulysses