How to Create Alias to Attributes in Ruby

How to set value to alias_attribute?

What alias_attribute does is it, well, aliases setter and a couple of getters for the attribute. New name does not become a real attribute. So, if using the original, unaliased name is not desirable, your only choice is to call the setter, not write_attribute.

USER_ATTRIBUTES = [:name, :foo]
USER_ATTRIBUTES.each do |field|
send("#{field}=", new_data[field])
# equivalent to
# self.foo = new_data(:foo)
end

As a bonus, this should also work for all other non-attribute setters, such as attr_accessors.

How to use FactoryGirl to create an attribute called alias?

Ruby doesn't know whether you're trying to call a method called alias or alias one method as another, and defaults to the latter. You can disambiguate by doing

self.alias "dummy"

ie, by explicitly specifying the receiver. This is usually the way to go in other cases where it is ambiguous whether you are calling a method or doing something else e.g.

self.foo = 'bar'

to call the foo= method rather than create a local variable called foo.

For a small number of field names this won't work. Factory girl's DSL uses method_missing so if the DSL object has a method of that name it will be called instead. In those cases you can do what the DSL sugar normal does for you and call add_attribute directly:

FactoryGirl.define do
factory :blah do
add_attribute :name, "some value"
end
end

is the same as

FactoryGirl.define do
factory :blah do
name "some value"
end
end

How to alias attribute if association exists in Rails model?

Perhaps there is a nice way to accomplish your goal with delegate

delegate :name, to: :company, prefix: true, allow_nil: true 

The options like :prefix => :fancy and :allow_nil => true allow you to flavor your delegated method as you like.

The above example makes the method company_name available. That might be useful in general, as well in an accessor others have suggested.

def name
company_name || super
end

More here https://apidock.com/rails/Module/delegate

Alias attribute ruby ActiveResource::Base

You can do a little of metaprogramming here:

module JavaAliasing
def initialize(hash)
super(Hash[hash.map do |k,v|
[k.to_s.gsub(/[a-z][A-Z]/) { |s| s.split('').join('_') }.downcase.to_sym, v]
end])
end
end

Let me illustrate this:

class Instantiator
def initialize(hash)
hash.each { |k,v| instance_variable_set "@#{k}", v }
end
end

Instantiator.new(asdf: 2).instance_variable_get('@asdf') #=> 2

class MyARModel < Instantiator
include JavaAliasing
end

MyARModel.new(asdfQWER: 2).instance_variable_get("@asdf_qwer") #=> 2

Here, a real life example (rails 4.0):

> Player.send :include, JavaAliasing
> Player.new(name: 'pololo', username: 'asdf', 'teamId' => 23)
=> #<Player id: nil, name: "pololo", username: "asdf", email: nil, type: "Player", created_at: nil, updated_at: nil, provider: nil, uid: nil, team_id: 23, last_login: nil, last_activity: nil>

Rails 4 - Generic alias_attribute

Apparently looks like was a misunderstanding of how should I invoke the attributes.
Instead of self.attribute_names should be attribute_names since the method should be called only for instantiated elements.

Also, after this first fix, I had to skip all the attributes with the same name, because this will cause SystemStackError (stack level too deep)

The final implementation should be like this:

# Defining alias attributes
attribute_names.each do |attr|
next if attr.blank? || (attr == attr.underscore)
alias_attribute attr.underscore.to_sym, attr.to_sym
end

Hope this helps someone else.
Also if a some have answer feel free to post it. Thanks

How to loop through all attributes and alias_attribute on all of them?

attributes is an instance method, but column_names is a class method that should suit your purpose.

class Apple < ActiveRecord::Base
Apple.column_names.each do |att|
alias_attribute :att.downcase, :att
end
end
end


Related Topics



Leave a reply



Submit