valibuk.net

A place for opinions and notes of a valibuk.

How To Handle Model In Module?

Tagged with: — ondrej at 10:53 pm on Sunday, February 4, 2007

Ruby on RailsThe number of models in your Ruby on Rails application is growing and one day you will get a brilliant idea.. Let’s put them to modules (that is to different subdirectories in the modules directory). Well, I had this day some weeks ago and, honestly, it is not a good idea… Helpers and tools in RoR, that work without any problems and manual changes when models do not use modules, will require some manual changes to work.

Here is a list of my experiences…

Paginate Helper

A model name for the paginate helper cannot be :model anymore, you have to write 'module/model'.

For a model without a module:

  1.  

For a model in a module:

  1.  

View

Any reference to a model has to contain also a module name: Model.content_columns has to be written as Module::Model.content_columns.

For a model without a module:

  1.  

For a model in a module:

  1.  

Scaffold

Files generated for a model in a module with the scaffold script do not work. It is necessary to fix the two above mentioned problems, that is to fix:

  • the paginate problem in a controller
  • the view problem in the list.rhtml file

I also tried the AJAXScaffold, but it relies that the model name does not contain also a module name. I tried to fix it, but I did not have a lucky day.

Generate

To create a model in a module with the generate script use the module/model notation, e.g.:

For a model without a module:

  1.  

For a model in a module:

  1.  

Model Relationship

If there is a relationship between models in two different modules, it is necessary to specify the :class_name option, otherwise the model in a different module will not be recognised.

For models not in a module:

  1.  

For models in different modules:

  1. span style="color:#996600;">"Library::Book"

That is all. To have a model in a module is solvable, but definitely not easy to use.
If you have a lot of models and you decided to store them in different modules, my comments may help you.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • DZone
  • Digg
  • Reddit
  • Technorati
  • Furl
  • NewsVine
  • Slashdot
  • Ma.gnolia
  • StumbleUpon

2 Comments »

Comment by MatthewRudy

June 3, 2007 @ 10:03 pm

I think you made this too hard for yourself.
Unless you were duplicating model names
eg. First::Model, and Second::Model,
you probably just wanted to keep your models in different directories.

so your app/models directory looks like;
first/
alpha.rb
beta.rb
gamma.rb
second/
one.rb
two.rb
three.rb

Rails will automatically expect second/one.rb to define Second::One.
however, you could just add “#{RAILS_ROOT}/app/models/second” to your load path in environment.rb
In this case, it’ll just look in these additional directories for normal models.
(the test files you can sort as you want, same with fixtures)

“config.load_paths += %W( #{path1} #{path2} )” would do it.

Comment by Daicoden

October 21, 2007 @ 8:13 pm

If you do that you screw up name spaces!

Is it possible to do

library/
book.rb
bookstore/
book.rb

and actually get it to work right?

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Comment Preview


commercial break :)

Make an account on slicehost.com -- a really good hosting where you have your own virtual machine. I installed Gentoo there = I like it very much ;)