Where do I put my own class?

If I do my own class, of something, and want to access it in my app /
controllers, where do I put it? Is /lib the place?

How do I access it?

It’s about Rails but at the same time more of a Ruby question, so I hope
that’s ok.

Ryan B. wrote:

Correct, lib is the place.

Thanks. And how do I access it in my controller?

Correct, lib is the place.

Just like any normal class (usually)… you may have to require the
file in config/environment.rb, but I think files in lib are
automatically loaded.

Depending on what type of class you’re thinking of: you can also
create a class in the app/models folder, even if your model won’t be
an ActiveRecord model (so no associated database table). Think of
using that if your class is really a model, so creating objects that
you want to view and manipulate in the usual MVC way. For example a
class of static objects that can be auto-generated at runtime (and
don’t need a database table because they will never change) like the
12 months of a year could be encapsulated in a model.

Dirk.

On 20 aug, 08:09, Pål Bergström [email protected]

Ryan B. wrote:

Just like any normal class (usually)… you may have to require the
file in config/environment.rb, but I think files in lib are
automatically loaded.

Perfect. :slight_smile:

deegee wrote:

Depending on what type of class you’re thinking of: you can also
create a class in the app/models folder, even if your model won’t be
an ActiveRecord model (so no associated database table). Think of
using that if your class is really a model, so creating objects that
you want to view and manipulate in the usual MVC way. For example a
class of static objects that can be auto-generated at runtime (and
don’t need a database table because they will never change) like the
12 months of a year could be encapsulated in a model.

Dirk.

On 20 aug, 08:09, P�l Bergstr�m [email protected]

I see. That makes sense. Just a thought, but perhaps that would be the
place to put different language variables? If a user set his/here
language settings to ‘swe’ all variables that is in there and in the app
would output the right word in Swedish instead of English – that I’ve
statically entered in a class.

yeah you r right .

you may have to require the file in config/environment.rb,

Nope, no need

but I think files in lib are automatically loaded.

Yes they are.

Hi,
Different language variables bundled in a class I would definetely put
in the lib folder like Ryan suggested; also if you want to override or
extend some standard Rails classes, that’s where you should do it. The
other options you have apply to more specific cases:

  • My suggestion to put some of your own classes in the app/models
    folder should only be done if your class is really a model (just not
    necessarily an ActiveRecord model). So a Class with objects that can
    be viewed and possibly manipulated by the user.
  • If you create a Module/Class with very generic behaviour that you
    want to reuse also in your other applications, make your own plugin
    (in vendor/plugins).

Cheers,
Dirk.

On 20 aug, 10:30, Pål Bergström [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs