If I want to add a class so I can create instances of that class in a
controller, where do I put the code to create the class?
There. I said it. It''s embarrassing to admit that I don''t
know the
answer to that question and it illustrates my poor understanding of
the overall RoR framework structure. So, if anyone can also point me
to something I could read that would enlighten me on the overall RoR
framework structure, I would be appreciative of that as well.
Thanks for any input.
... doug
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
doug wrote:> If I want to add a class so I can create instances of that class in a > controller, where do I put the code to create the class?If the class is not an ActiveRecord model, then place it in a file in the application''s "lib" directory. The filename should be the lower-cased version of the class name with words separated by underscores (MyClass => lib/my_class.rb). When you access "MyClass", Rails will automatically know how to find the definition and load it in. -- 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 -~----------~----~----~----~------~----~------~--~---
I''m not entirely sure I''ve understood the question, but if you
class
is called Foo, then it should live in app/models/foo.rb
You can then call Foo.create or Foo.new from anywhere you want. If you
want your own methods for creating new instance you could write
class Foo < ActiveRecord::Base
def self.make_me_a_new_one
.....
end
end
and then call Foo.make_me_a_new_one from anywhere you want.
Fred
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
That works, but I prefer to separate models and new classes.
I usually put extra classes in "extras/" and then uncomment the
following in environment.rb:
# Add additional load paths for your own custom dirs
config.load_paths += %W( #{RAILS_ROOT}/extras )
That way my new classes and the database-derived models are not
intermixed. It''s up to you, really. You can use lib, or extras, so
long as rails can see it and your project makes sense to you and your
team.
-john
On Mar 22, 2008, at 2:52 PM, Frederick Cheung wrote:
>
> I''m not entirely sure I''ve understood the question, but
if you class
> is called Foo, then it should live in app/models/foo.rb
> You can then call Foo.create or Foo.new from anywhere you want. If you
> want your own methods for creating new instance you could write
>
> class Foo < ActiveRecord::Base
> def self.make_me_a_new_one
> .....
> end
> end
>
> and then call Foo.make_me_a_new_one from anywhere you want.
>
> Fred
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---