I’m working on my first Rails app. It needs to store some information about a single advertisement that will appear on every page. The admin just needs to be able to set the URL, Title, and an image. Obviously, I only need one instance of this ad object.
I created a model that inherits from ActiveRecod::Base, but that seems like the wrong thing to do, since it is configured to save multiple ads in a database table.
What’s the best way for me to do this? What should the model and controller look like?
Thanks in advance,
Avi
I would suggest that you continue using ActiveRecord but that you add a boolean attribute that determines which of the many advertisement records is the active one. I have called this field active within the following example.
You can then validate within the model that a maximum of one record is active. The validation of the active attribute should succeed under any of the following conditions
The following class should meet your needs