Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1045 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alias for column names in Rails

#1
In my database has column names such as 'delete' or 'listen-control' and so on. These cannot be changed, so I would like to alias the names so as to avoid problems in my application.

I found [the following code][1] but it is outdated (05 Aug 2005) and doesn't work with Rails 3:

module Legacy
def self.append_features(base)
super
base.extend(ClassMethods)
end
module ClassMethods
def alias_column(options)
options.each do |new_name, old_name|
self.send(:define_method, new_name) { self.send(old_name) }
self.send(:define_method, "#{new_name}=") { |value| self.send("#{old_name}=", value) }
end
end
end
end

ActiveRecord::Base.class_eval do
include Legacy
end

How can I alias the column names? Is it possible?

[1]:

[To see links please register here]

Reply

#2
As Jaime said, those names might cause problems.

In that case, use some sensible names. Your GUI should never dictate how your columns are named.

Suggestions: `is_deleted` or `deleted_at`, `listen_control`

Then, change your view accordingly, that's way easier than fighting ActiveRecord and your database.
Reply

#3
Aliasing method names won't solve your problem. As I mentioned in my comment above, you can't have dashes in ruby method or variable names, because ruby will interpret them as a "minus". so:

object.listen-control

will be interpreted by ruby as:

object.listen - control

and will fail. The code snippet you found might be failing because of ruby 1.9, not rails 3. Ruby 1.9 doesn't let you call `.send` on protected or private methods anymore, like 1.8 used to.

That being said, I do understand there are times when old database column names don't look very nice, and you want to clean them up. Create a folder in your lib folder called "bellmyer". Then create a file called "create_alias.rb", and add this:

module Bellmyer
module CreateAlias
def self.included(base)
base.extend CreateAliasMethods
end

module CreateAliasMethods
def create_alias old_name, new_name
define_method new_name.to_s do
self.read_attribute old_name.to_s
end

define_method new_name.to_s + "=" do |value|
self.write_attribute old_name.to_s, value
end
end
end
end
end

Now in your model that needs aliasing, you can do this:

class User < ActiveRecord::Base
include Bellmyer::CreateAlias
create_alias 'name-this', 'name_this'
end

And it will alias properly. It's using the `read_attribute` and `write_attribute` methods of ActiveRecord to access those table columns without calling them as ruby methods.
Reply

#4
Declare this in your model.

alias_attribute :new_column_name, :column_name_in_db
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through