Sorry if this is a complete newbish question, but I''m trying to wrap my head around creating custom classes in my rails webapp. I''m normally a Java developer, but I figured I''d give RoR a try and see how I like it, and whether it would be useful for production use with a new client. Anyways, what are the best practices for custom classes? Where and/or how do I create and use them? Is there a tutorial somewhere? Basically, here''s what I''m trying to do. I have an XML file in a custom format that represents news articles. In my controller I''m using REXML to read and parse that file (BTW, see my note at the end of this email), and I want to encapsulate that into an "Article" type class, with title, date, description, etc that I can then use in my view. How do I go about this? Where should I store the class? If someone could point me to a tutorial or example of custom classes in a Rails app, or give me a little hint how/where to start I''d really appreciate it. Thanks, - Brent NOTE: I was having a terrible time with RoR giving me a "File not found" error when calling File.new. It said the current directory was my rails project directory root. I checked, rechecked, copied-pasted the directory/file to make SURE it was there. It was! Turns out the current directory is the config/ directory and NOT the project one as reported.
Am Montag, den 20.03.2006, 10:37 -0500 schrieb Brent Johnson:> Sorry if this is a complete newbish question, but I''m trying to wrap > my head around creating custom classes in my rails webapp. I''m > normally a Java developer, but I figured I''d give RoR a try and see > how I like it, and whether it would be useful for production use with > a new client. > > Anyways, what are the best practices for custom classes? Where and/or > how do I create and use them? Is there a tutorial somewhere? > Basically, here''s what I''m trying to do. I have an XML file in a > custom format that represents news articles. In my controller I''m > using REXML to read and parse that file (BTW, see my note at the end > of this email), and I want to encapsulate that into an "Article" type > class, with title, date, description, etc that I can then use in my > view. > > How do I go about this? Where should I store the class? If someone > could point me to a tutorial or example of custom classes in a Rails > app, or give me a little hint how/where to start I''d really appreciate > it.I would suggest storing your Article model in the app/models directory and adding a create_from_xml class method, or parse the xml in the controller as you already do. -- Norman Timmler http://blog.inlet-media.de
If you''ve not read the Agile Web Development book, you really should. YOu can define custom classes anywhere, really... *lib, vendor, etc. However, what you have is definitely a model class... So create it in the models folder. App/models/article.rb class Article # class methods def self.find() # load a new article instance from the xml file based on the conditions in the finder. # maybe you have to return an array of these article objects because you might find # more than one. end def self.find_by_title end # instance methods def title '' return title end ... End Then you can access it like any other model. Please share what you create... It could be useful to others! Good luck! Brian Hogan Web Development Learning & Technology Services Schofield 3-B University of Wisconsin-Eau Claire 715 836 3585 hoganbp@uwec.edu -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Brent Johnson Sent: Monday, March 20, 2006 9:38 AM To: rails@lists.rubyonrails.org Subject: [Rails] Newbie Question about Custom Classes Sorry if this is a complete newbish question, but I''m trying to wrap my head around creating custom classes in my rails webapp. I''m normally a Java developer, but I figured I''d give RoR a try and see how I like it, and whether it would be useful for production use with a new client. Anyways, what are the best practices for custom classes? Where and/or how do I create and use them? Is there a tutorial somewhere? Basically, here''s what I''m trying to do. I have an XML file in a custom format that represents news articles. In my controller I''m using REXML to read and parse that file (BTW, see my note at the end of this email), and I want to encapsulate that into an "Article" type class, with title, date, description, etc that I can then use in my view. How do I go about this? Where should I store the class? If someone could point me to a tutorial or example of custom classes in a Rails app, or give me a little hint how/where to start I''d really appreciate it. Thanks, - Brent NOTE: I was having a terrible time with RoR giving me a "File not found" error when calling File.new. It said the current directory was my rails project directory root. I checked, rechecked, copied-pasted the directory/file to make SURE it was there. It was! Turns out the current directory is the config/ directory and NOT the project one as reported. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks for the response. I was just thinking of doing this. But here''s what I would normally do in Java. I''d create a News class, and an Article class. The News class would read the XML and create an array of Article classes. So, should I create two models, one called News and one called Articles? Is every custom class considered a model? What about some utility class that really doesn''t fit as a model? Thanks again, - Brent On 3/20/06, Norman Timmler <lists@inlet-media.de> wrote:> I would suggest storing your Article model in the app/models directory > and adding a create_from_xml class method, or parse the xml in the > controller as you already do. > > -- > Norman Timmler > > http://blog.inlet-media.de > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Am Montag, den 20.03.2006, 10:57 -0500 schrieb Brent Johnson:> Thanks for the response. I was just thinking of doing this. But > here''s what I would normally do in Java. I''d create a News class, and > an Article class. The News class would read the XML and create an > array of Article classes. > > So, should I create two models, one called News and one called > Articles? Is every custom class considered a model? What about some > utility class that really doesn''t fit as a model? > > Thanks again, > > - Brent > > On 3/20/06, Norman Timmler <lists@inlet-media.de> wrote: > > I would suggest storing your Article model in the app/models directory > > and adding a create_from_xml class method, or parse the xml in the > > controller as you already do.Ok, i''ll get a little bit deeper: Classes, that hold data go into app/models. Classes that have nothing to do with holding data, go into the lib directory. If your parsing logic is not too complex, i would put it into the Article class. If it is complex, put it as a separate class or module in lib. My suggestion for your Article class: class Article # class methods class < self def build_from_xml # parsing logic goes here returning an Array of Article objects end end end In your controller just do: class NewsController < ApplicationController def my_action File.open(''my_articles.xml'') do |file| @articles = Article.build_from_xml(file.read) end end end -- Norman Timmler http://blog.inlet-media.de
Thanks for the info! The parsing won''t be terribly complex (just doing some xpath queries, etc) so I think I''ll use your suggestion. In Java, for a class to be used it has to be in your classpath. How does RoR figure out where the actual class is? Does it look under your app/models (and controllers, views, etc) and lib directories? Thanks again, - Brent On 3/20/06, Norman Timmler <lists@inlet-media.de> wrote:> Ok, i''ll get a little bit deeper: > > Classes, that hold data go into app/models. Classes that have nothing to > do with holding data, go into the lib directory. > > If your parsing logic is not too complex, i would put it into the > Article class. If it is complex, put it as a separate class or module in > lib. > > My suggestion for your Article class: > > class Article > > # class methods > class < self > def build_from_xml > # parsing logic goes here returning an Array of Article objects > end > end > end > > In your controller just do: > > class NewsController < ApplicationController > def my_action > File.open(''my_articles.xml'') do |file| > @articles = Article.build_from_xml(file.read) > end > end > end > > -- > Norman Timmler > > http://blog.inlet-media.de > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I did a CMS framework for a newspaper in Java, using the Spring J2EE framework. There, I created an XML DAO layer, and then had XMLbean NewsML business objects instantiated by the controller accessing the DAO layer (which talked to an Xindice server!). (By the way, it''s been working in production for ages, stable as heck... WISH RAILS 1.0 HAD BEEN THERE THEN... took me months to develop, instead of the weeks it would have taken on Rails, not to mention some javascript sprite action that would have been a heck of a lot simpler!). The great thing about treating the persistent XML (whether files, a server like XIndice, etc.) as a data access layer meant that if later I decided to use MySQL or Berkeley XML database, I would only have to modify that layer. Now... On Rails, just simply have your controller instantiate a business object (a model class) that knows how to read/write XML from/to flat files. This can be by invoking a library, or, to get more sophisticated, by writing a data access adapter for flat XML files (ActiveRecord/ConnectionAdapters) (my Rails ignorance prevents me from knowing whether this already exists as a third party; the API doesn''t mention any). Hope that helps. Victor Kane http://awebfactory.com.ar Date: Mon, 20 Mar 2006 10:37:36 -0500> From: "Brent Johnson" <bljohnson@gmail.com> > Subject: [Rails] Newbie Question about Custom Classes > To: rails@lists.rubyonrails.org > Message-ID: > <9a27dace0603200737r7392a8c3t8624a438cabb450d@mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Sorry if this is a complete newbish question, but I''m trying to wrap > my head around creating custom classes in my rails webapp. I''m > normally a Java developer, but I figured I''d give RoR a try and see > how I like it, and whether it would be useful for production use with > a new client. > > Anyways, what are the best practices for custom classes? Where and/or > how do I create and use them? Is there a tutorial somewhere? > Basically, here''s what I''m trying to do. I have an XML file in a > custom format that represents news articles. In my controller I''m > using REXML to read and parse that file (BTW, see my note at the end > of this email), and I want to encapsulate that into an "Article" type > class, with title, date, description, etc that I can then use in my > view. > > How do I go about this? Where should I store the class? If someone > could point me to a tutorial or example of custom classes in a Rails > app, or give me a little hint how/where to start I''d really appreciate > it. > > Thanks, > > - Brent > > NOTE: I was having a terrible time with RoR giving me a "File not > found" error when calling File.new. It said the current directory was > my rails project directory root. I checked, rechecked, copied-pasted > the directory/file to make SURE it was there. It was! Turns out the > current directory is the config/ directory and NOT the project one as > reported. >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060320/9c39294d/attachment-0001.html
Am Montag, den 20.03.2006, 11:27 -0500 schrieb Brent Johnson:> In Java, for a class to be used it has to be in your classpath. How > does RoR figure out where the actual class is? Does it look under > your app/models (and controllers, views, etc) and lib directories?Ruby has a loadpath. It is extended by Rails (1.0) using the following method: def default_load_paths paths = ["#{root_path}/test/mocks/#{environment}"] # Then model subdirectories. # TODO: Don''t include .rb models as load paths paths.concat(Dir["#{root_path}/app/models/[_a-z]*"]) paths.concat(Dir["#{root_path}/components/[_a-z]*"]) # Followed by the standard includes. # TODO: Don''t include dirs for frameworks that are not used paths.concat %w( app app/models app/controllers app/helpers app/services app/apis components config lib vendor vendor/rails/railties vendor/rails/railties/lib vendor/rails/actionpack/lib vendor/rails/activesupport/lib vendor/rails/activerecord/lib vendor/rails/actionmailer/lib vendor/rails/actionwebservice/lib ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) } end Files in this directories don''t have to be required explicitly (because of some Ruby extensions made by rails), but files in subdirectories of you loadpath, e.g. lib/my_library/myfile.rb must be required like this: require ''my_library/myfile'' -- Norman Timmler http://blog.inlet-media.de
Alright, I know I have to be doing something extremely noobish. But I''ve created a simple class that has one method just as a test, because RoR can''t seem to find the methods I put in the class. So here''s the simplest example I could think of: class Article def blah end end Yes, this does absolutely nothing. But if I try this in my controller: Article.blah() I get "undefined method `blah'' for #<Article:0xb789bfbc>". It doesn''t matter what I put into "blah" it just never sees it. It does, however, see the article class. Do I have to do anything special, or define the method another way so that it''s accessible from the controller? It''s currently in app/models/article.rb. Thanks, - Brent
>>>>> "Brent" == Brent Johnson <bljohnson@gmail.com> writes:> Alright, I know I have to be doing something extremely noobish.Kind of, yes. It''s a Ruby thing rather than a Rails thing you''re missing.> class Article > def blah > end > end> Yes, this does absolutely nothing. But if I try this in my controller:> Article.blah()> I get "undefined method `blah'' for #<Article:0xb789bfbc>".That''s because you''ve defined an instance method but are trying to call a class method. That''s not going to work, as you''ve noticed. With the code as you''ve written it here, you can call it like this... a = Article.new a.blah ...or, if you want to call it as a class method, you do like this... class Article def self.blah end end ...and then you can call Article.blah -- Calle Dybedahl <calle@cyberpomo.com> http://www.livejournal.com/users/cdybedahl/ "But that was in the gauzy realm of meat and oxygen, not this real world here on the screen." -- archbishopm, LiveJournal
> That''s because you''ve defined an instance method but are trying to > call a class method. That''s not going to work, as you''ve noticed. With > the code as you''ve written it here, you can call it like this... > > a = Article.new > a.blahTried that first, actually. I get the same result, undefined method.> ...or, if you want to call it as a class method, you do like this... > > class Article > def self.blah > end > endJust tried that, but got the same message. I know I''ve missed something hugely obvious somewhere. I wasn''t sure if the problem was Ruby OR Rails related, but I looked at some examples and tutorials on creating classes in Ruby and it looks pretty straight forward. So I thought maybe there was something in Rails I had to do to get it to work correctly. Thanks, - Brent
OK, here''s the deal. It turns out I''ve got some caching turned on or something. I bounced Apache HTTPD and wham, it worked. I then added a return to my blah function to return a string. I tried again and it wasnt returning anything. I bounced the server again and it returned the value I set. Seems as though it gets cached the first time it runs then never changes again. Thanks for the help, - Brent On 3/20/06, Brent Johnson <bljohnson@gmail.com> wrote:> > That''s because you''ve defined an instance method but are trying to > > call a class method. That''s not going to work, as you''ve noticed. With > > the code as you''ve written it here, you can call it like this... > > > > a = Article.new > > a.blah > > Tried that first, actually. I get the same result, undefined method. > > > ...or, if you want to call it as a class method, you do like this... > > > > class Article > > def self.blah > > end > > end > > Just tried that, but got the same message. I know I''ve missed > something hugely obvious somewhere. I wasn''t sure if the problem was > Ruby OR Rails related, but I looked at some examples and tutorials on > creating classes in Ruby and it looks pretty straight forward. So I > thought maybe there was something in Rails I had to do to get it to > work correctly. > > Thanks, > > - Brent >
Where is your class file located? -- -- Tom Mornini On Mar 20, 2006, at 11:06 AM, Brent Johnson wrote:>> That''s because you''ve defined an instance method but are trying to >> call a class method. That''s not going to work, as you''ve noticed. >> With >> the code as you''ve written it here, you can call it like this... >> >> a = Article.new >> a.blah > > Tried that first, actually. I get the same result, undefined method. > >> ...or, if you want to call it as a class method, you do like this... >> >> class Article >> def self.blah >> end >> end > > Just tried that, but got the same message. I know I''ve missed > something hugely obvious somewhere. I wasn''t sure if the problem was > Ruby OR Rails related, but I looked at some examples and tutorials on > creating classes in Ruby and it looks pretty straight forward. So I > thought maybe there was something in Rails I had to do to get it to > work correctly. > > Thanks, > > - Brent > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Am Montag, den 20.03.2006, 14:06 -0500 schrieb Brent Johnson:> > That''s because you''ve defined an instance method but are trying to > > call a class method. That''s not going to work, as you''ve noticed. With > > the code as you''ve written it here, you can call it like this... > > > > a = Article.new > > a.blah > > Tried that first, actually. I get the same result, undefined method. > > > ...or, if you want to call it as a class method, you do like this... > > > > class Article > > def self.blah > > end > > end > > Just tried that, but got the same message. I know I''ve missed > something hugely obvious somewhere. I wasn''t sure if the problem was > Ruby OR Rails related, but I looked at some examples and tutorials on > creating classes in Ruby and it looks pretty straight forward. So I > thought maybe there was something in Rails I had to do to get it to > work correctly.What about what i suggested before: class Article # class methods class < self def blah end end end Don''t forget to restart your Websever unless you are working in development mode using webrick server. -- Norman Timmler http://blog.inlet-media.de
Brent: You say you''re using Apache? Why not use WEBrick? The reason I ask is that if you are using Apache + FCGI and you are in "Production" mode, all models will be cached. And there are memory leaks with FCGI and "Development" mode. If you''re just starting out, I''d recommend just using WEBrick. (Better debug messages in my opinion). -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Brent Johnson Sent: Monday, March 20, 2006 1:13 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Newbie Question about Custom Classes OK, here''s the deal. It turns out I''ve got some caching turned on or something. I bounced Apache HTTPD and wham, it worked. I then added a return to my blah function to return a string. I tried again and it wasnt returning anything. I bounced the server again and it returned the value I set. Seems as though it gets cached the first time it runs then never changes again. Thanks for the help, - Brent On 3/20/06, Brent Johnson <bljohnson@gmail.com> wrote:> > That''s because you''ve defined an instance method but are trying to > > call a class method. That''s not going to work, as you''ve noticed. > > With the code as you''ve written it here, you can call it like > > this... > > > > a = Article.new > > a.blah > > Tried that first, actually. I get the same result, undefined method. > > > ...or, if you want to call it as a class method, you do like this... > > > > class Article > > def self.blah > > end > > end > > Just tried that, but got the same message. I know I''ve missed > something hugely obvious somewhere. I wasn''t sure if the problem was > Ruby OR Rails related, but I looked at some examples and tutorials on > creating classes in Ruby and it looks pretty straight forward. So I > thought maybe there was something in Rails I had to do to get it to > work correctly. > > Thanks, > > - Brent >_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
It''s located in app/models. I was thinking it may be running in the production environment, because there is a setting called "config.cache_classes" which was set to true. But I tried setting that to false and restarting and still get the same result. - Brent On 3/20/06, Tom Mornini <tmornini@infomania.com> wrote:> Where is your class file located? > > -- > -- Tom Mornini > > On Mar 20, 2006, at 11:06 AM, Brent Johnson wrote: > > >> That''s because you''ve defined an instance method but are trying to > >> call a class method. That''s not going to work, as you''ve noticed. > >> With > >> the code as you''ve written it here, you can call it like this... > >> > >> a = Article.new > >> a.blah > > > > Tried that first, actually. I get the same result, undefined method. > > > >> ...or, if you want to call it as a class method, you do like this... > >> > >> class Article > >> def self.blah > >> end > >> end > > > > Just tried that, but got the same message. I know I''ve missed > > something hugely obvious somewhere. I wasn''t sure if the problem was > > Ruby OR Rails related, but I looked at some examples and tutorials on > > creating classes in Ruby and it looks pretty straight forward. So I > > thought maybe there was something in Rails I had to do to get it to > > work correctly. > > > > Thanks, > > > > - Brent > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Well, personally, I like developing, testing and pushing the same files in the same environment. So if the production server is apache, then I develop with the same. But, if there are known issues I could always switch to WEBrick for development, and leave Apache for production. I turned the caching options off in config/environments/*.rb for all environments, restarted Apache, and it still caches. Thanks, - Brent On 3/20/06, Hogan, Brian P. <HOGANBP@uwec.edu> wrote:> Brent: You say you''re using Apache? Why not use WEBrick? The reason I > ask is that if you are using Apache + FCGI and you are in "Production" > mode, all models will be cached. And there are memory leaks with FCGI > and "Development" mode. > > If you''re just starting out, I''d recommend just using WEBrick. (Better > debug messages in my opinion).
It''s the beauty of Rails.... You can develop on WEBRICK and SQLite and deploy to Apache + MySQL or Lighttpd + Oracle without worrying about anything breaking. We use WEBrick for day-to-day development, stage to Apache to make sure it works, and then deploy to production on Apache. -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Brent Johnson Sent: Monday, March 20, 2006 1:23 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Newbie Question about Custom Classes Well, personally, I like developing, testing and pushing the same files in the same environment. So if the production server is apache, then I develop with the same. But, if there are known issues I could always switch to WEBrick for development, and leave Apache for production. I turned the caching options off in config/environments/*.rb for all environments, restarted Apache, and it still caches. Thanks, - Brent On 3/20/06, Hogan, Brian P. <HOGANBP@uwec.edu> wrote:> Brent: You say you''re using Apache? Why not use WEBrick? The reason I > ask is that if you are using Apache + FCGI and you are in "Production"> mode, all models will be cached. And there are memory leaks with FCGI > and "Development" mode. > > If you''re just starting out, I''d recommend just using WEBrick. (Better> debug messages in my opinion)._______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Well one problem I may have is name based virtual hosting. If I have two or more projects in development at the same time, using apache I can just use name based virtual hosting on our dev server and have one web server serving two different sites (which is how we also do it in production). But if I use WEBrick for development, will I have to load two instances of it on different ports? Thanks, - Brent On 3/20/06, Hogan, Brian P. <HOGANBP@uwec.edu> wrote:> It''s the beauty of Rails.... You can develop on WEBRICK and SQLite and > deploy to Apache + MySQL or Lighttpd + Oracle without worrying about > anything breaking. > > We use WEBrick for day-to-day development, stage to Apache to make sure > it works, and then deploy to production on Apache. > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Brent Johnson > Sent: Monday, March 20, 2006 1:23 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Newbie Question about Custom Classes > > > Well, personally, I like developing, testing and pushing the same files > in the same environment. So if the production server is apache, then I > develop with the same. But, if there are known issues I could always > switch to WEBrick for development, and leave Apache for production. > > I turned the caching options off in config/environments/*.rb for all > environments, restarted Apache, and it still caches. > > Thanks, > > - Brent > > On 3/20/06, Hogan, Brian P. <HOGANBP@uwec.edu> wrote: > > Brent: You say you''re using Apache? Why not use WEBrick? The reason I > > ask is that if you are using Apache + FCGI and you are in "Production" > > > mode, all models will be cached. And there are memory leaks with FCGI > > and "Development" mode. > > > > If you''re just starting out, I''d recommend just using WEBrick. (Better > > > debug messages in my opinion). > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
OK, even using WEBrick the class gets cached. The caching is all set to false under config/environments, is there something I''m missing? - Brent On 3/20/06, Hogan, Brian P. <HOGANBP@uwec.edu> wrote:> It''s the beauty of Rails.... You can develop on WEBRICK and SQLite and > deploy to Apache + MySQL or Lighttpd + Oracle without worrying about > anything breaking. > > We use WEBrick for day-to-day development, stage to Apache to make sure > it works, and then deploy to production on Apache.
Yes you would run two WEBrick servers on different ports. I guess I''ve never run into a situation on my local machine where that''s been an issue; if I need that, then I deploy to staging. Whatever works for you! As for your caching... I''m baffled. Perhaps you have the environment set to production in the environment file? -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Brent Johnson Sent: Monday, March 20, 2006 1:39 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Newbie Question about Custom Classes Well one problem I may have is name based virtual hosting. If I have two or more projects in development at the same time, using apache I can just use name based virtual hosting on our dev server and have one web server serving two different sites (which is how we also do it in production). But if I use WEBrick for development, will I have to load two instances of it on different ports? Thanks, - Brent On 3/20/06, Hogan, Brian P. <HOGANBP@uwec.edu> wrote:> It''s the beauty of Rails.... You can develop on WEBRICK and SQLite and> deploy to Apache + MySQL or Lighttpd + Oracle without worrying about > anything breaking. > > We use WEBrick for day-to-day development, stage to Apache to make > sure it works, and then deploy to production on Apache. > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Brent > Johnson > Sent: Monday, March 20, 2006 1:23 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Newbie Question about Custom Classes > > > Well, personally, I like developing, testing and pushing the same > files in the same environment. So if the production server is apache,> then I develop with the same. But, if there are known issues I could > always switch to WEBrick for development, and leave Apache for > production. > > I turned the caching options off in config/environments/*.rb for all > environments, restarted Apache, and it still caches. > > Thanks, > > - Brent > > On 3/20/06, Hogan, Brian P. <HOGANBP@uwec.edu> wrote: > > Brent: You say you''re using Apache? Why not use WEBrick? The reason > > I ask is that if you are using Apache + FCGI and you are in > > "Production" > > > mode, all models will be cached. And there are memory leaks with > > FCGI and "Development" mode. > > > > If you''re just starting out, I''d recommend just using WEBrick. > > (Better > > > debug messages in my opinion). > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 3/20/06, Hogan, Brian P. <HOGANBP@uwec.edu> wrote:> Yes you would run two WEBrick servers on different ports. I guess I''ve > never run into a situation on my local machine where that''s been an > issue; if I need that, then I deploy to staging. > > Whatever works for you!Yeah, using separate ports will be an acceptable solution. If there''s some weirdness when it moves from development to staging then I''ll deal with it then. I think it''ll be fine though.> As for your caching... I''m baffled. Perhaps you have the environment set > to production in the environment file?The only thing uncommented out in environment.rb is: require File.join(File.dirname(__FILE__), ''boot'') Rails::Initializer.run do |config| end That''s it. And here is what''s uncommented in the environment files: development.rb ---------------- config.cache_classes = false config.whiny_nils = true config.breakpoint_server = true config.action_controller.consider_all_requests_local = true config.action_controller.perform_caching = false config.action_mailer.raise_delivery_errors = false test.rb -------- (all the same as above except the last line) config.action_mailer.delivery_method = :test production.rb --------------- config.cache_classes = false config.action_controller.consider_all_requests_local = false config.action_controller.perform_caching = false And that''s it. But it still insists on caching the class when I change it. Here''s the kicker, I can change the controller all day long and it doesn''t cache it. So, for example, my "blah" method just returns a static string, I then set @page_title to what it returns before it''s handed off to my layout, partials and view. If I change what article.rb returns it doesn''t change (appears to be cached), BUT if I change the controller and just append "BRENT" to what it returns, THAT change appears but the text that prepends that is still the old cached string from the blah call. It''s driving me nuts. I''m definitely NOT going to restart the server after every little change to a class. Thanks, - Brent
Alright I just ran into something that''s exactly what I''m talking about when I say a difference in environment by not using the same webserver. I switched to WEBrick and a piece of my code stopped working. When I make this call: file = File.new("public/news.xml") It works fine using WEBrick, because the rails root is the current working directory. HOWEVER, under Apache this is not the case. Rails REPORTS that the root is the cwd, even in the file not found error, but I went crazy trying to figure out why it couldn''t find it because I knew it was there. Turns out when running under Apache, the cwd is in a subdirectory of the root. If I had to guess I''d say the cwd was the "config" directory and not the actual rails root. So my code was set to look for it in "../public/news.xml" and that worked perfectly fine. As soon as I switch to WEBrick, bang, error. Anyone else use Apache and notice this? - Brent On 3/20/06, Hogan, Brian P. <HOGANBP@uwec.edu> wrote:> Yes you would run two WEBrick servers on different ports. I guess I''ve > never run into a situation on my local machine where that''s been an > issue; if I need that, then I deploy to staging. > > Whatever works for you! > > As for your caching... I''m baffled. Perhaps you have the environment set > to production in the environment file?
Brent Johnson wrote:> OK, even using WEBrick the class gets cached. The caching is all set > to false under config/environments, is there something I''m missing?For Rails to reload your model files, the file name must correspond to the class name, as follows: class Article should be in app/models/article.rb class NewsItem should be in app/models/news_item.rb You hadn''t mentioned your file names, so I thought I should point this out. regards Justin
Yep, the filename of the Article model is app/models/article.rb. I''m still trying to figure out why it''s being cached under WEBrick and Apache both. Thanks, - Brent On 3/20/06, Justin Forder <justin@justinforder.me.uk> wrote:> Brent Johnson wrote: > > > OK, even using WEBrick the class gets cached. The caching is all set > > to false under config/environments, is there something I''m missing? > > For Rails to reload your model files, the file name must correspond to > the class name, as follows: > > class Article should be in app/models/article.rb > > class NewsItem should be in app/models/news_item.rb > > You hadn''t mentioned your file names, so I thought I should point this out. > > regards > > Justin > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >