dimanche 6 août 2017

Rails 3 - Splat attr_accessible with Active Record

I'm making typedoptions dynamic by moving them to a new model, as you can see before I was using a constant to make the columns t_a, t_b, t_c accessible with *TYPED_DATA, but now that the model has been migrated, i can't do it anymore. Is there a something I'm missing.

By the way, this fails when I've re-run my specs with FactoryGirl.

class Rules < ActiveRecord::Base
  #TYPED_DATA = %w{a b c}.map { |t| t.prepend('t_').to_sym }

  attr_accessible :name, *Typed.prefixed
end

# typed.rb
class Typed < ActiveRecord::Base
  def self.prefixed
    Typed.pluck(:name).map { |name| name.prepend('t_').to_sym }
  end
end

What is the best option to dynamically expose those attributes?

Aucun commentaire:

Enregistrer un commentaire