rails-settings icon indicating copy to clipboard operation
rails-settings copied to clipboard

NoMethodError: undefined method `default_settings' for #<Class:0x007f83b8c57ab8>

Open adiakritos opened this issue 7 years ago • 3 comments

It appears that the target class isn't being assigned a default_settings hash. Why might this be the case?

I've tried it on two different models in our codebase.

Using Rails 3.2.22 and Ruby 2.3.3

adiakritos avatar Nov 20 '17 18:11 adiakritos

Does a normal call (such as @record.settings(:some_configured_type)) work? Interested to see whether there's a general problem with the gem loading or whether it's specific to the default_settings hash.

Which gem version are you using?

olliebennett avatar Jun 16 '18 16:06 olliebennett

I'm having a similar problem using 2.4.3. Here's an example:

shop = Shop.first
settings = RailsSettings::SettingObject.find_by(target_id: shop.id, target_type: "Shop")
# settings returns RailsSettings::SettingObject with all settings thats good
# settings.discount also returns good data
settings.blank? # this gives error, also .present? etc

Error:

NoMethodError: undefined method `default_settings' for #<Class:0x00007f9bc74aac58>
Did you mean?  default_extensions
from /Users/benjam1n/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activerecord-5.2.0/lib/active_record/dynamic_matchers.rb:22:in `method_missing'

benpixel avatar Aug 01 '18 13:08 benpixel

That's strange...

  • Have you restarted the server (or spring for example) after installing the gem?

  • Have you configured the relevant default values in has_settings? Here's my user config;

    # user.rb
    has_settings do |s|
      s.key :email_notifications, defaults: {
        promotions: false,
        newsletters: true
      }
    end
    

    Then I can do the following;

    [1] pry(main)> User.first.default_settings
      User Load (6.8ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]
    => {:email_notifications=>
       "promotions"=>false,
       "newsletters"=>true}
    

Are you able to configure a model in the same, minimal way and see if you can reproduce?

olliebennett avatar Aug 01 '18 14:08 olliebennett