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

Equivalent to ActiveRecord::Store stored_attributes #64

Open
brianlittmann opened this issue Apr 10, 2015 · 0 comments
Open

Equivalent to ActiveRecord::Store stored_attributes #64

brianlittmann opened this issue Apr 10, 2015 · 0 comments

Comments

@brianlittmann
Copy link

In case someone comes looking for it, here is the equivalent of getting the list of attributes (keys) for a given hstore. It's briefly mentioned in the README (bottom paragraph), but wasn't initially clear for me.

# ActiveRecord::Store
class Event < Content
    store_accessor :mydata, :name, :cost, :sponsor
end

Event.stored_attributes[:mydata]
# => [:name, :cost, :sponsor]
# Hstore Accessor gem
class Event < Content
    hstore_accessor :mydata, name: :string, cost: :string, sponsor: :string
end

Event.hstore_metadata_for_mydata.keys
# => [:name, :cost, :sponsor]

For STI cases, I added a method on the parent class to get the keys and fail safely if a child class does not have any hstore attributes defined.

class Content < ActiveRecord::Base
    def self.mydata_attribute_names
        self.hstore_metadata_for_mydata.keys rescue []
    end
    delegate :mydata_attribute_names, to: :class
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants