vendredi 1 janvier 2016

Rails - generate a Scaffold within a Namespace

I have an early stage project that I am trying to namespace before organization becomes a problem, following the most clear and up to date post I can find regarding namespacing.

But it doesn't work as magically as I'd hoped. How do I generate a Scaffold within a Namespace that is functional? I believe this is an issue of plurality vs. singularity.

Example:

words
  languages
  definitions

users

Generate:

rails g scaffold Words/Language name:string
rails g scaffold Words/Definition name:string
rails g scaffold Word name:string language:references definition:references user:references

After the above, there is an exception for word.language but not word.user:

irb(main)> word = Word.find(1)
  Word Load (0.3ms)  SELECT  "words".* FROM "words" WHERE "words"."id" = ? LIMIT 1  [["id", 1]]
=> #<Word id: 1, name: "Hello", language_id: 1, definition_id: 1, user_id: 1>

irb(main)> word.user
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
=> #<User id: 1, name: "admin">

irb(main)> word.language
NameError: uninitialized constant Word::Language

Rails generates two models, both word.rb and words.rb:

app/models/word.rb

class Word < ActiveRecord::Base
  belongs_to :language
  belongs_to :definition
  belongs_to :user
end

app/models/words.rb

module Words
  def self.table_name_prefix
    'words_'
  end
end

How do I combat this issue of singular vs. plural with namespaced generated scaffolds?

Aucun commentaire:

Enregistrer un commentaire