0

I am using strong_parameters gem in my rails project. To apply that to all the models i have created an initializer and put the below code.

 ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)

Now i don't want to apply it for a model called votes. please help me to exclude that model?

Can Can
  • 3,644
  • 5
  • 32
  • 56

1 Answers1

0

I would suggest you include the module for every model you want. What you are doing is monkey-patching ActiveRecord which is not a good idea.

UPDATE

Here is a solution where you can include ActiveModel::ForbiddenAttributesProtection only for models you like. Put the code in an initializer.

all_models = Dir["#{Rails.root}/app/models/**/*.rb"].map do |filename| 
  filename[/(?<=models\/).*(?=\.rb)/].camelize.constantize
end

models_without_forbidden_attributes_protection = [Foo] # put your filtered classes here

all_models.each do |model|
  unless models_without_forbidden_attributes_protection.include?(model)
    model.send(:include, ActiveModel::ForbiddenAttributesProtection)
  end
end
hahcho
  • 1,369
  • 9
  • 17
  • for small size projects it's ok. But if the project contains more than 100 models it's not a good choice. – Can Can Jan 08 '15 at 11:36
  • Would it be fine to just iterate all over your models and include it for them? – hahcho Jan 08 '15 at 11:44
  • can't we exclude a single class? – Can Can Jan 08 '15 at 11:54
  • Excluding is not a feature that is easy applicable. You cannot just remove the `module` you must remove all of its methods and restore any previous definitions you have for the clashing methods. You can iterate over all the models in an initializer and filter out the ones you do not want to include the module and include it only for the models you want. – hahcho Jan 08 '15 at 11:58
  • Just put it in initializer of your choice. – hahcho Jan 09 '15 at 11:25