views:

82

answers:

2

I have column LTD in my Company model. After retrieving value from model using

Company.find

If any value is present in LTD column, then I have to display the text "Limited" on the view. I have many columns in the model which are in the abbreviated form and when value is present their long form are displayed on the view. Therefore writing conditions on the view is not feasible.

I was thinking whether writing a custom rails config file containing application constants will do. But I don't have quantitative and qualitative information on this.

Please help. Thanks in advance.

+1  A: 

You could create a separate Abbreviation model that your Company model could be associated with through a join model CompanyAbbreviation. Then there would be one join table record for each column in a specific company record. Rather than having each abbreviation as a column in your companies table you would have secondary keys in your company_abbreviations table referring to the associated company and abbreviation records.

Something like the following:

class Company < ActiveRecord::Base
  has_many :company_abbreviations
  has_many :abbreviations, :through => :company_abbreviations
end

class Abbreviation < ActiveRecord::Base
  has_many :company_abbreviations
end

class CompanyAbbreviation < ActiveRecord::Base
  belongs_to :company
  belongs_to :abbreviation
end

class CreateAbbreviations < ActiveRecord::Migration
  def self.up
    create_table :abbreviations do |t|
      t.string :abbr
      t.string :description
    end

    add_index :abbreviations, :abbr
  end
end

class CreateCompanyAbbreviations < ActiveRecord::Migration
  def self.up
    create_table :company_abbreviations do |t|
      t.references :company
      t.references :abbreviation
    end

    add_index :company_abbreviations, :company_id
    add_index :company_abbreviations, :abbreviation_id
  end
end

In db/seeds.db you could pre-populate your abbreviations table.

You add new associations like this:

@company.company_abbreviations.create(:abbreviation => Abbreviation.find_by_abbr("LTD"))

In your view you can reference the expanded abbreviation columns cleanly like this:

<% @company.abbreviations.each do |abbr| %>
  <%= abbr.description %>
<% end %>

You may also want to control the display order in some fashion, say by a sort column in the join table,

bjg
@bjg I don't want an extra model..as I don't want to create an extra overhead of maintaining Abbreviations and their long form.
Rohit
@Rohit You realize that, if you want to support abbreviations and expansions, you will need to maintain them in some fashion or another. Either in a lookup table or in code (which is really a codified lookup table). If you don't want to put them in the database you could consider a class-based approach. The overhead is actually low. One time setup followed by incremental updates as new abbreviations need to be supported. The problem with a column based approach is that it is highly unnormalized and will arguably incur more associated overhead to maintain.
bjg
A: 

This works for me perfectly.

I have declared a global hash in config/environment.rb which maintains the list of all the column name short-forms and long-forms and on the view I just check if value is present in the column I search for the corresponding key value pair from the global hash and display the long-form.

Thanks guyz for giving your time to help me.

Rohit