DmitryTsepelev / store_model

Work with JSON-backed attributes as ActiveRecord-ish models

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Can attributes be accessed programatically?

toxaq opened this issue · comments

commented

Should it be possible to access the attribute details programatically as per a normal ActiveRecord class?

ie should the following work (or is there another way)

class Configuration
  include StoreModel::Model
  attribute :model, :string
  attribute :foo, :string
end

Configuration.attribute_names
=> ["model", "foo"]

I can do it ok on an instance with

Configuration.new.attributes.keys
=> ["model","foo"]

I use this so that any new attributes are available via the API.

Hi @toxaq, I've just checked and Configuration.attribute_names should work perfectly fine. Doesn't it work in your case?

commented

Hi @DmitryTsepelev, no, it's not working at my end. I'm using 0.8 of the gem. I cant think what would be different between our setups to cause this.

class Tenant::Location
  include StoreModel::Model

  attribute :address1, :string
  attribute :address2, :string
  attribute :suburb, :string
  attribute :city, :string
  attribute :state, :string
  attribute :postcode, :string
end

In console

=> Tenant::Location
irb(main):003:0> Tenant::Location.attribute_names
Traceback (most recent call last):
    1: from (irb):3
NoMethodError (undefined method `attribute_names' for Tenant::Location:Class)
Did you mean?  attribute_types
           attribute_alias
           attribute_aliases
           attribute_types=
           attribute_types?
irb(main):004:0> 

Thoughts on what I could check? We are still on Rails 5.2 if that matters.

Oh, Attributes API was a bit different in 5.2. There were no such public method, but maybe attribute_types will be helpful?

commented

Well now I do feel silly... We are halfway through the Rails 6 upgrade so I must be confusing branches. Thank you for your responses, this gem and all the Evil Martians stuff you do. Big fan!