0

I'd like to create a model in my rails application that will be stored in my database (or in any other way) and which will be unique.

The usual way to create models in rails creates a whole table, but that seems overkill to store only one record...

Is there a better way to do this ?

EDIT:

I think my question needs improvements to be fully answered to.

I want to use a model because it will permit to upload an image with carrierwave, which will be unique and displayed in a specific place on my website.

Like a logo for exemple

Shrolox
  • 663
  • 6
  • 22
  • Will this model have attributes that are updated on the fly and need to be accessed across different instances of rails? – Michael Gorman Jun 20 '17 at 13:28
  • I'd like it to behave like a regular activerecordmodel, so yes. – Shrolox Jun 20 '17 at 13:31
  • and will there be multiple servers with multiple instances? – Michael Gorman Jun 20 '17 at 13:34
  • I don't think so – Shrolox Jun 20 '17 at 13:35
  • Something you may consider then is using [this answer](https://stackoverflow.com/a/6126706/8088139) to set up a seperate DB connection via something like sqlite for this one model so that it is stored as a file, while the rest of your models use your default db. This would allow it to maintain updates across multiple instances while still being faster and lighter than a full db model – Michael Gorman Jun 20 '17 at 13:37
  • 1
    [this question and its answers](https://stackoverflow.com/q/399447/8088139) look to be similar to yours – Michael Gorman Jun 20 '17 at 13:46
  • Thanks. I think singleton was the word missing from my vocabulary – Shrolox Jun 20 '17 at 13:49
  • You can have a `Configuration` table and polymorphic models (I presume you may want other singleton model elsewhere in your application ?) so you have the power of ActiveRecord and you are not forced to create a new table each time you want something like this. – Aschen Jun 20 '17 at 14:22

1 Answers1

0

Do you really need an ActiveRecord model ?
If it's just to store some informations you can use config_for :

Start with creating you configuration file in config/custom.yml

development:
  foo: 42
  bar: "Hello local"

production:
  foo: 4200
  bar: "Hello world"

Now load you custom configuration in config/application.rb in order to make it available in your apps :

module MyApps
  class Application < Rails::Application

    config.custom = config_for :custom # will load config/custom.yml

  end
end

Finally you can access it anywhere in your code like this :

Rails.configuration.custom['foo'] #=> 42
Aschen
  • 1,691
  • 11
  • 15