Rails ActiveRecord Database with One Row

Is there a way for the database to allow only one row (for example, for site parameters)?

+4
source share
3 answers
class Whatever < ActiveRecord::Base validates :there_can_only_be_one private def there_can_only_be_one errors.add_to_base('There can only be one') if Whatever.count > 0 end end 
+5
source

In Rails 4:

 class Anything < ActiveRecord::Base before_create :only_one_row private def only_one_row false if Anything.count > 0 end end 

Silent mistakes then

 class Anything < ActiveRecord::Base before_create :only_one_row private def only_one_row raise "You can create only one row of this table" if Anything.count > 0 end end 
+3
source

Is there only one column in this row? If not, adding new columns with migration may be excessive. You could at least make this table contain the columns "name" and "value" and check its uniqueness by name.

+2
source

All Articles