3

I have a Rails 3.2.8 model that is in a namespace. I'm sure it used to be that the namespace was automatically expanded out into what the table name would look like. e.g. Module::Model would become module_model in the database.

For some reason I have a problem now that this does not happen through the rails application. The migrations, models and controllers all live in a namespace but when the model looks for a table it ignores the table prefix and complains that it cannot find the database.

Here is the example

module Magazine
  def self.table_name_prefix
    'magazine_'
  end
end

module Magazine
  class Paper < ActiveRecord::Base
    #some stuff here
  end
end

When I do a query on Magazine::Paper it looks for the table "paper" and not "maganzine_paper" which it should. This is causing the whole application to exibit some weird behaviour. I have also tried setting the table name manually in the model with self.table_name but this only lets the model find the right table. Routes still play games with me as when I nest something below papers for example comments then it looks for a route that does not exist.

This is what is inside the routes. Comments should go to /magazine/papers/1/comments but it looks for a route called /magazine/comments

namespace :magazine do
  resources :papers do 
    resources :comments
  end
end

What is going on?

Update:

Ok so I figured it out but not sure if I should ignore it or see whats causing it. The engine name is magazine and in the engine I create a namespace for models called magazine but this causes the problems. So rails can't have a namespace name similar to the engine name?

Cœur
  • 37,241
  • 25
  • 195
  • 267
pieterk
  • 31
  • 1
  • 3
  • I created a new Rails project and generated a model, and it works like a charm in 3.2.8. Could you provide your directory structure of `app/models`? – KARASZI István Aug 13 '12 at 10:29
  • Ok so here's the strange thing. I tried creating a project from scratch using no database switches (default sqlite3) and it works. I suspect I am totally messing something up with the mysql version or its broken. Probably the former. Thanks for all the comments so far though. – pieterk Aug 14 '12 at 17:27

2 Answers2

3

I ran into the same problem, forgot my application's name (main module) had the same name as a namespace for my models. Changing either, the module name or the application name solves this issue.

guapolo
  • 2,443
  • 2
  • 20
  • 19
  • This is obviously a Rails bug. It is really scary it wasn't fixed almost one year later. – Wilson Freitas Jul 16 '14 at 22:02
  • Ran into this as well. When your app is called "Foo" then app/models/foo.rb is not loaded at all (probably because Foo class is already defined?) – Pascal Oct 11 '14 at 11:37
0

If you just write

namespace :magazine do
 resources :papers do 
  resources :comments
 end
end

and run rake routes you can see it is looking for proper urls and when you say it is looking for /magazine/comments then you surely must have written another routes somewhere in the file. When you write

module Magazine
 def self.table_name_prefix
  'magazine_'
 end
end

And when you write your model with the same module name Magazine then you are replacing the same module with different contents within it. Instead of writing model Paper within Magazine module just include the module within the model Paper in following way.

class Paper < ActiveRecord::Base
  include Magazine
  #some stuff here
end

This will ensure your table_name_prefix method to be called when your model is loaded when your application starts.

Shreyas Agarwal
  • 1,128
  • 8
  • 10
  • I still get the problem where it looks for the table name without prefix. This is weird as namespaces and prefixes worked amazingly in rails 3.1 – pieterk Aug 13 '12 at 10:13
  • Please remove method def self.table_name_prefix and just write this self.table_name_prefix = 'magazine_' as it is a class variable and not a class method – Shreyas Agarwal Aug 13 '12 at 10:58
  • Using the self.table_name_prefix = 'magazine_' but when visiting the page it shows the error Could not find table 'pages' which I assume should be 'magazine_pages' – pieterk Aug 13 '12 at 14:39
  • 2
    Ok so I figured it out but not sure if I should ignore it or see whats causing it. The engine name is magazine and in the engine I create a namespace for models called magazine but this causes the problems. So rails can't have a namespace name similar to the engine name? – pieterk Aug 27 '12 at 16:35
  • 2
    I just had the same issue... except instead of an engine, I named my namespace the same as a module in a gem. And BAM...didn't work.. I removed the gem, worked like a charm... Put the gem back, broken.. sounds like rails has got some issues there. On 4.1.0.beta1 – noli Jan 05 '14 at 06:43
  • @pieterk, I'm having the same issue with engine namespaces, did you ever find anything else about this? – dax Apr 27 '15 at 09:14