Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting.save_default does not work when defaults[] is used in the model #47

Closed
vanboom opened this issue Aug 27, 2014 · 2 comments
Closed

Comments

@vanboom
Copy link

vanboom commented Aug 27, 2014

When defaults are placed in the Setting model...
class Setting < RailsSettings::CachedSettings
defaults[:test] = "Test Setting"
end

a call to...
Setting.save_default(:test, "A new Setting") returns nil and does not save the setting to the database.

Suggest modifying the software so save_default overrides and creates the database record.
thanks,
Don

@seaneshbaugh
Copy link

I was baffled by the same issue but I believe I have found the cause and a (sort of less than ideal) solution.

It appears this problem is being caused by spring which keeps the Rails application loaded and therefore keeps the cache around. I had truncated the settings table at some point so I could reset it with new defaults. Afterwards my new settings weren't taking in the application or showing up in the database so I tried to mimic the behavior of #save_default but with some extra output by doing the following

if Setting.application_title.nil?
  puts 'Setting application_title.'
  Setting.application_title = 'My Application'
end

just to make sure something weird wasn't going on. Setting.application_title wasn't returning nil so the setting wasn't being set. I discovered that when I added Rails.cache.delete('settings:application_title') before the above that it worked just fine. So of course the normal call to #save_default worked just fine as well. So if you're using spring (which ships with Rails 4.1 by default) you might need to clear the cache for your settings before you use #save_default.

@mtozlu
Copy link

mtozlu commented Sep 4, 2015

@seaneshbaugh Thank you very much for this. I had the same problem and your explanation saved me.

@huacnlee huacnlee closed this as completed Jul 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants