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

Creation working only after calling Model.all #214

Open
jawakarD opened this issue Oct 8, 2020 · 1 comment
Open

Creation working only after calling Model.all #214

jawakarD opened this issue Oct 8, 2020 · 1 comment

Comments

@jawakarD
Copy link

jawakarD commented Oct 8, 2020

I have a model called Hotel which is using active yaml.

class Hotel < ActiveYaml::Base
  set_root_path Rails.root.join('db', 'yaml_data')
  set_filename 'hotel'
end

After going to rails c if I do Hotel.create(name: "A hotel name") it's throwing

NoMethodError (undefined method `name=' for #<Hotel:0x00007f7ff0ce7b98 @attributes={:name=>"name"}>)  

Anyway if i do the same after running Hotel.all. It's just working fine:

vailability-validation ~/repos/booking-engine> rails c
Running via Spring preloader in process 6236
Loading development environment (Rails 6.0.3.2)

irb(main):001:0> Hotel.create(name: "name")
Traceback (most recent call last):
           1: from (irb):1
NoMethodError (undefined method `name=' for #<Hotel:0x00007f7ff17106d8 @attributes={:name=>"name"}>)

irb(main):002:0> Hotel.all
 => #<ActiveHash::Relation:0x00007f7ff71e6300 @klass=Hotel, @all_records=[#<Hotel:0x00007f7ff71ef900 @attributes={:id=>1, :....... @query_hash={}, @records_dirty=false>

irb(main):003:0> Hotel.create(name: "name")
=> #<Hotel:0x00007f7ff710bb88 @attributes={:name=>"name", :id=>5}>

I have just used Hotel model for simplicity, but this is happenning on all yaml models.

@chrisbloom7
Copy link

This is documented in the readme https://github.com/zilkey/active_hash#auto-defined-fields. The fields are autoloaded once you call Model.all. To make them available before that, use the fields class method.

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

2 participants