Rails 4 How to Call Accessible_Attributes from Model

Rails 4 how to call accessible_attributes from Model

Actually accessible_attributes fetch that columns which are declare attr_accessible in model but in rails 4 they have removed the attr_accessible from model and used the strong_parameter instead of that so for this make an method of same name accessible_attributes in his model then inside that method declare that columns array which are you want. Such as:

def accessible_attributes
[col1_name, col2_name, ....]
end

Rails 4 : Able to modify non-accessible attributes

strong_parameters protect you from mass assignment in this specific controller. It does not protect your from using the mass assignment elsewhere in the application or in the console.

So it does not protect the model like attr_accessible did. But this is how it should work. It is not the model's job to protect itself. It is the controller's job to protect the model. And there might be an Admin::UsersController that allows more than this UsersController.

Calling accessor methods from within a Model object in Rails

When you define an attribute in ActiveRecord, the following methods are available

# gets the value for needs_review
def needs_review
end

# sets the value for needs_review
def needs_review=(value)
end

You can call the setter using

needs_review = "hello"

but this is the same way you set a variable. When you call the statement within a method, Ruby gives higher precedence to variables assignment, thus a variable with that name will be created.

def one
# variable needs_review created with value foo
needs_review = "foo"
needs_review
end

one
# => returns the value of the variable

def two
needs_review
end

two
# => returns the value of the method needs_review
# because no variable needs_review exists in the context
# of the method

As a rule of thumb:

  1. always use self.method_name = when you want to call the setter within a method
  2. optionally use self.method_name when a local variable with the same name exists in the context

How is attr_accessible used in Rails 4?

Rails 4 now uses strong parameters.

Protecting attributes is now done in the controller. This is an example:

class PeopleController < ApplicationController
def create
Person.create(person_params)
end

private

def person_params
params.require(:person).permit(:name, :age)
end
end

No need to set attr_accessible in the model anymore.

Dealing with accepts_nested_attributes_for

In order to use accepts_nested_attribute_for with strong parameters, you will need to specify which nested attributes should be whitelisted.

class Person
has_many :pets
accepts_nested_attributes_for :pets
end

class PeopleController < ApplicationController
def create
Person.create(person_params)
end

# ...

private

def person_params
params.require(:person).permit(:name, :age, pets_attributes: [:name, :category])
end
end

Keywords are self-explanatory, but just in case, you can find more information about strong parameters in the Rails Action Controller guide.

Note: If you still want to use attr_accessible, you need to add protected_attributes to your Gemfile. Otherwise, you will be faced with a RuntimeError.

Why aren't Rails model attributes accessible using symbols instead of strings?

The Hash returned when you call #attributes on a AR instance has string keys, which is why a symbol as an index into the hash doesn't work in your case. There is a subclass of Hash called HashWithIndifferentAccess which automatically converts symbol indexes into strings.

Quite often in Rails you'll encounter HashWithIndifferentAccess instances. A perfect example is the params variable you access in your controller and view code.

Try using work_effort.attributes.with_indifferent_access[key]

Really it is just doing the same thing that you are, but it does it behind the scenes.

Rails create a method accessible via both the model and controller

Instead of using a constant (VALID_MODULES), try making it an attribute of your job.

class Job < ActiveRecord::Base

attr_accessor :valid_modules
after_initialize :init

validates :name, presence: true
validates :desc, presence: true
validates :api, presence: true, :inclusion => { :in => VALID_MODULES}
validates :filters, presence: true
validates :toe, presence: true

def init
@valid_modules ||= []
Dir.foreach('lib/resources') do |item|
next if ['.', '..', 'resource.rb'].include?(item)
#Wont be called very often so O(n) complexity is fine (small #elements)
@valid_modules << item[0..-4] unless @valid_modules.include?(item[0..-4])
end
end

end

Now in your controller you can just call valid_modules on your Job object to return the array.
Example:

job = Job.new
job.valid_modules

How I can set 'attr_accessible' in order to NOT allow access to ANY of the fields FOR a model using Ruby on Rails?

Just set:

class Users < ActiveRecord::Base
attr_accessible #none
end

Like Pan Thomakos said (attr_accessible is the array of parameters that can be mass-ret. So if you send in no symbols, then no parameters will be accessible.

This ticket was useful

Rails ActiveRecord method to list attributes including associated virtual attributes

In my book, the correct way is to use attr_accessible to define accessible attributes. Then on your model you can simply call Foo.accessible_attributes and get a nice list.

However, associations are trickier, but you can do something like:

Foo.accessible_attributes.to_a + Foo.reflect_on_all_associations.map(&:name)

If you aren't using attr_accessible you'd have to hack it together with

Bundle.new.attributes.keys - Bundle.protected_attributes.to_a

How do you discover model attributes in Rails?

For Schema related stuff

Model.column_names         
Model.columns_hash
Model.columns

For instance variables/attributes in an AR object

object.attribute_names                    
object.attribute_present?
object.attributes

For instance methods without inheritance from super class

Model.instance_methods(false)


Related Topics



Leave a reply



Submit