Marcin Rzeźnicki
2013-Apr-11 13:54 UTC
[ActiveRecord] Attribute aliasing is driving me nuts :-)
Hi folks!
Quick question about alias_attribute in context of legacy database. I am on
AR 3.2.11. I got very weird, but easy to follow, naming scheme to deal
with. That is - every attribute is suffixed by ''_''. I thought
I dealt with
that cleverly, that is, I introduced common ''abstract'' base
class for my
models:
class PersistentObject < ActiveRecord::Base
self.abstract_class = true
#...
def self.define_attribute_methods
super
attribute_names.each do |old_name|
next if old_name == primary_key
if old_name == locking_column
alias_attribute ''version'', old_name
else
new_name = old_name.chomp(''_'')
new_name.downcase!
alias_attribute new_name, old_name if new_name != old_name
end
end
end
end
Basic things do work, I can refer to something called
''FLOWER_'' by
model.flower, also some validators work correctly, eg. this one is fine:
validates_length_of :name, :category, maximum: 255, allow_nil: true
But, as always, not all that glitters is gold. Some things fail miserably
eg. :
validates_uniqueness_of :name, scope: :category
throws weird errors about nil not responding to a method etc. That escaped
my logic until I debugged what was really going on ( (I''ll get back to
this
later). You can fix that by using:
validates_uniqueness_of :NAME_, scope: :CATEGORY_
Ugly, but works. Same things goes for dynamic finders. #find_by_name
refuses to cooperate #find_by_NAME_ is fine. I read in various places
about this problem and, honestly, didn''t like what I found out
(euphemism
for - I found that mightily stupid :-)). Then I started figuring this out
by myself trying to judge what it takes to make at least unique validator
work. After a bit of debugging I nailed that down to:
#activerecord/validations/uniqueness.rb:56
#build_relation
column = klass.columns_hash[attribute.to_s]
Needles to say columns_hash cares nothing about my aliases and happily
tries to procede with nil. Ok, so I modified my aliasing method to actually
pack the stuff I needed into columns_hash, luckily at the point of aliasing
columns''ve already been created so I could just put copies under
respective
new keys. That let me go past this issue but when I saw generated query I
was stunned - it used wrong names in all the places. Madness. I gave up.
So, my question is: maybe you know someone who has taken on this issue and
released a solution? Or maybe there are plans to fix this on AR level? Or,
the most welcome possibility, maybe this is dead simple to fix :-) I
appreciate all the answers, thanks in advance.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/JUlt3NUoCsYJ.
For more options, visit https://groups.google.com/groups/opt_out.