Rockman
Rockman

Reputation: 99

Rails 4, strong_parameters, dynamic attr_accessible: how to get parent db info into class instance

I'm trying to add attr_accessible to a User model based on its parent Account. I know the the user class instance doesn't yet have access to its parent, but I can't figure out how to do this. Any suggestions would be awesome.

This is how I'm getting strong_params to work, and would like to do similar in User Model (or do something better).

#appilcation_controller
  ...
  def config_permitted_parameters
   ...
   devise_parameter_sanitizer.for(:account_update) { |u| u.permit(*user_attributes)}
  end

  def user_attributes
    attrs = [:name, :email, :password, :password_confirmation, :current_password, :primary_account, :account_id]
    @account.user_attributes.each do |att|
      attrs << att[0].to_sym
    end
    attrs
  end

This is what works now:

#User.rb

%w[no_call_list informal_name].each do |key|
  attr_accessible key
  store_accessor :properties, key
  scope "has_#{key}", lambda { |value| where("properties @> hstore(?, ?)", key, value) }
end

This is what I would like to work:

#User.rb

#@account.user_attributes.each do |key|
#current_user.account.user_attributes.each do |key|
  attr_accessible key
  store_accessor :properties, key
  scope "has_#{key}", lambda { |value| where("properties @> hstore(?, ?)", key, value) }
end

Upvotes: 1

Views: 223

Answers (0)

Related Questions