2

Dealing with a legacy system that has non-Rails conventional naming. Note that all tables and attributes are UPPERCASE, which in the sqlserver-adapter means that ID is NOT the same as id.

I had thought that alias_attribute :new, :OLD allowed you to specify a name that you could use in ActiveRecord/ActiveRelation queries. From what I'm seeing below (tested in rails console), that is not the case.

The end goal is making the legacy system "act" in a Rails-conventional methodology by making each model have an ID attribute, etc...

Model definition:

# app/models/organization.rb
class Organization < ActiveRecord::Base
  self.table_name = "ORGANIZATION"
  self.primary_key = "ORGANIZATION_ID"

  alias_attribute :id, :ORGANIZATION_ID
end

Does not work:

Organization.select(:id) => invalid column name 'id'

Organization.select(:ID) => invalid column name 'ID'

Organization.select("ID") => invalid column name 'ID'

Does work:

Organization.select(:organization_id) => <finds record>

Organization.select(:ORGANIZATION_ID) => <finds record>

Organization.select("organization_id") => <finds record>

Organization.select("ORGANIZATION_ID") => <finds record>

Dan L
  • 4,319
  • 5
  • 41
  • 74

2 Answers2

0

I just used alias_attribute the other day. Honestly, I kept poking at it until I got mine to work.

I was able to achieve the functionality that you are looking for by reversing the params passed to alias_attribute.

Example:

alias_attribute :active?, :active
alias_attribute :alert, :message
alias_attribute :delete_at, :expire_at

Where the first param is what the column name is, and the second is what you are aliasing. I understand that seems backwards from documentation, however this is how I got this to work.

gamesfreke
  • 105
  • 1
  • 1
  • 9
  • I tried switching them and got the same problem. With regards to your particular examples, Rails already adds a "?" to the end of each attribute and returns a boolean value. You shouldn't need to make the query attributes yourself. – Dan L Sep 26 '14 at 12:58
0

I solved this situation by configuring the DB adapter (MS SQL Server) to treat the schema as all lowercase. Thus, even though the actual DB table names can be "USERS", "PEOPLE", etc..., I can reference them as "users", "people" in my application.

See: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter#force-schema-to-lowercase

Here's the code I added to resolve this:

# config/initializers/sql_server_adapter.rb
ActiveRecord::ConnectionAdapters::SQLServerAdapter.lowercase_schema_reflection = true
Dan L
  • 4,319
  • 5
  • 41
  • 74