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?
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?
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.
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.