1

I have blog with only one author. Author encapsulates many different fields that will appear on every page. Do I Need an Author Model?

Where should I store my author (I use MongoDB) and how can I do that in the rails way?

nkm
  • 5,844
  • 2
  • 24
  • 38
Dmitry Zhlobo
  • 369
  • 2
  • 10

2 Answers2

1

If your blog is designed to be this way, and there never ever will be another author, then you don't need to store this data in a DB (provided that it is relatively static).

You can create a "model" class and hardcode all values in it.

class Author

  def self.name
    "Sergio"
  end

  def self.email
    "sergio@example.com"
  end

end

Or, of course, you can use real model and actually store data in the database.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
1

Essentially, you want a model with only one row? The easiest way would probably be to do just that. In your seeds.rb file, just create a new author: Author.create :name => "Dmitry", :rails_skill => 9001 That way, you can always access it with Author.first, and just don't ever write code to create a new author anywhere.

That feels kind of odd though. If you're building this for yourself and don't care about the ability to add new authors in the future, you could either hardcode the author information everywhere it needs to be printed (gross) or build a custom initializer to set the author information and access it something like Sergio's answer.

wjl
  • 7,143
  • 1
  • 30
  • 49
  • Yes, I want a model with only one row. Is it normal to store my author in database and call `Author.first` enywhere? I will have document with only one entry. – Dmitry Zhlobo Feb 21 '12 at 21:54
  • I guess I don't know if that'd be "normal" or not, but I would do it that way. As long as you put that create in your seeds.rb, it'll be Author.first. – wjl Feb 21 '12 at 23:21