views:

39

answers:

3

Hello.

I write web-GUI for application and must (!) using names for columns such as 'delete' or 'listen-control' and so on.

I find this code but it is dated by 05 Aug 2005 and not works with Rails 3.

How can I redefine this names using native Rails mechanism? Is it possible?

PS I'm know about problem with hyphens in the Rails.

PPS This names dictates by another application, not me. This application writes and reads rows from/to database only with some hardcoded names. So I must using this names for my web-GUI. It's not my caprice.

A: 

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.

Ariejan
… and change main application which reads and writes in/from database with only this names?
+2  A: 

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.

Jaime Bellmyer
Thanks for answer, but I'm tested your solution and get error:SyntaxError (/usr/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0/lib/active_model/attribute_methods.rb:229: syntax error, unexpected ',' send(:vm-password=, *args) ^):when defined: alias_attribute "vm_password", "vm-password"and try to get "vm_password".
I found a solution! See my answer's edit above.
Jaime Bellmyer
Big thanks!I tested it and finds:1. In user.rb needs add first line: require 'bellmyer/create_alias.rb'2. When no one records in table, your method works fine. With one or more records rails gets error: ActionView::Template::Error (/usr/lib64/ruby/gems/1.9.1/gems/activemodel-3.0.0/lib/active_model/attribute_methods.rb:273: syntax error, unexpected '-', expecting keyword_end undef :vm-password?…3. With field name such as 'delete' this method not works because 'delete' is method name.
You can't use the old field names in your view. In my example above, you need to use "<%= user.name_this %>". You also need to custom_alias your delete field, and give it a different name. If you alias it to "delete_field" then you can call "<%= user.delete_field %>".
Jaime Bellmyer
I'm used new names of course.My migration: http://paste.pocoo.org/show/281427/My controller: http://paste.pocoo.org/show/281428/My model: http://paste.pocoo.org/show/281430/My view: http://paste.pocoo.org/show/281433/Error: http://paste.pocoo.org/show/281434/Erro for enable in migration and aliased 'delete': http://paste.pocoo.org/show/281439/Sorry for many pastes.
A: 

There's this method. Works well for me. Declare it in your model.

alias_attribute :new_column_name, :column_name_in_db
Shreyas Satish
This method doesn't work when a table column has a name with a dash in it. The table column has to map to a valid ruby method name as-is, to be able to use this.
Jaime Bellmyer