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

Is defining "dynamic" attributes possible? #192

Open
priverop opened this issue Jan 12, 2023 · 1 comment
Open

Is defining "dynamic" attributes possible? #192

priverop opened this issue Jan 12, 2023 · 1 comment

Comments

@priverop
Copy link

priverop commented Jan 12, 2023

Hello, I'm considering using this gem but I don't know if this is possible to do:

class Person
  include ActiveAttr::Attributes

  attribute :first_name
  attribute :last_name
  # we retreive data from the database with ActiveRecord
  Languages.all.each do |lang| 
      attribute :"name_#{lang.name}", type: String
  end
end

person = Person.new
person.first_name = "Chris"
person.last_name = "Griego"
person.name_english = "Christian"
person.name_spanish = "Cristian"
person.attributes #=> {"first_name"=>"Chris", "last_name"=>"Griego", "name_english" => "Christian", "name_spanish" => "Cristian"}

I don't know if this is it: #6

@priverop priverop changed the title Defining "dynamic" attributes Is defining "dynamic" attributes possible? Jan 12, 2023
@mathieujobin
Copy link

it should be possible, there is a chicken&egg issue where you need Languages to be loaded earlier. but I still think you can do it.

Also, you might need to wrap the dynamic part in a class << eval block

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