Merge Attribute after Rails find query

Hi Everyone,

   I have small doubt in rails. I hope i will get solution from here.

I am using rails find query like this

  def self.data_query(params,label)      BaseManagedEntity.find(:all,        :select => "b.BaseManagedEntityInternalId, c.#{DEVICE_NAMES[label]}, c.#{DEVICE_IPS[label]}, s.HealthState, s.LastModified",        :joins => "as b INNER JOIN #{TABLE_NAMES[label]} as c ON c.basemanagedentityid = b.basemanagedentityid                    INNER JOIN state as s ON b.basemanagedentityid = s.basemanagedentityid",        :conditions => BaseManagedEntity.all_conditions(params, label),        :order => "s.LastModified DESC",        :group => "b.BaseManagedEntityInternalId, s.HealthState, s.LastModified, c.#{DEVICE_NAMES[label]}, c.#{DEVICE_IPS[label]}" )   end

  DEVICE_NAMES = {"windows" => "NetworkName", "unix" => "NetworkName_360E5A02_BC9E_0000_2614_1972E304088A",                   "network" => "Name_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F"}

  DEVICE_IPS = {"windows" => "IPAddress", "unix" => "IPAddress_360E5A02_BC9E_0000_2614_1972E304088A",                   "network" => "IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F"}

  TABLE_NAMES = {"windows" => "mt_computer", "unix" => "MTV_Computer_0",                    "network" => "mt_networkdevice"}

  HEALTH_STATES = {"Normal" => 1, "Warning" => 2, "Critical" => 3, "Healthy" => 1 }

The abvoe method will call from this method

  def self.all_devices(params)     all_devices = data_query(params, "windows") + data_query(params, "unix") + data_query(params, "network")     all_devices.paginate((params[:page] || 1), CONSTANTS['PAGINATE'] ['TEN'] )   end

I want to merge a attribute called "device_type"( which i will define as attr_accessor) after find query based on the label i used so that i can use attribute in the views.

How can i do that. Please help me in this issue.

In ruby if you have a hash, for e.g. : hash = {} then you can say this: hash.merge!(“name” => “something”) this will append that label inside the hash.

Yeah,

  Can we do that after find query that i mentioned above. I want to apply for all records that i got from find query.   How can i do that ?

Since in your case, object belongs to the class of your model where you have defined attr_accessor attribute. All you have to do is just say @object_name.attribute_name = “data you want to display!” in the action you are calling and then same attribute will be available in its template too.