Rails Looping with different attributes

Hi Everyone,

   I am trying to solve this problem, but couldn't able to get the
solution. I hope i will get some help here.

I am using these three find methods to get different types of
devices. I dont have any relationships between any models.

  windows_devices = BaseManagedEntity.find(:all,
  :select => "b.BaseManagedEntityInternalId, c.NetworkName,
c.IPAddress, s.HealthState, s.LastModified",
  :joins => "as b INNER JOIN mt_computer as c ON
c.basemanagedentityid = b.basemanagedentityid
        INNER JOIN state as s ON b.basemanagedentityid =
s.basemanagedentityid",
  :conditions => "IsManaged = 1 and s.LastModified = (select
MAX(LastModified) from state where basemanagedentityid =
s.basemanagedentityid)",
  :order => "s.LastModified DESC",
  :group => "b.BaseManagedEntityInternalId, s.HealthState,
s.LastModified, c.NetworkName, c.IPAddress" )

  unix_devices = BaseManagedEntity.find(:all,
  :select => "b.BaseManagedEntityInternalId,
c.NetworkName_360E5A02_BC9E_0000_2614_1972E304088A,
c.IPAddress_360E5A02_BC9E_0000_2614_1972E304088A, s.HealthState,
s.LastModified",
  :joins => "as b INNER JOIN MTV_Computer_0 as c ON
c.basemanagedentityid = b.basemanagedentityid
        INNER JOIN state as s ON b.basemanagedentityid =
s.basemanagedentityid",
  :conditions => "IsManaged = 1 and s.LastModified = (select
MAX(LastModified) from state where basemanagedentityid =
s.basemanagedentityid)",
  :order => "s.LastModified DESC",
  :group => "b.BaseManagedEntityInternalId, s.HealthState,
s.LastModified, c.NetworkName_360E5A02_BC9E_0000_2614_1972E304088A,
c.IPAddress_360E5A02_BC9E_0000_2614_1972E304088A" )

  network_devices = BaseManagedEntity.find(:all,
  :select => "b.BaseManagedEntityInternalId,
c.Name_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F,
c.IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F, s.HealthState,
s.LastModified",
  :joins => "as b INNER JOIN mt_networkdevice as c ON
c.basemanagedentityid = b.basemanagedentityid
        INNER JOIN state as s ON b.basemanagedentityid =
s.basemanagedentityid",
  :conditions => "IsManaged = 1 and s.LastModified = (select
MAX(LastModified) from state where basemanagedentityid =
s.basemanagedentityid)",
  :order => "s.LastModified DESC",
  :group => "b.BaseManagedEntityInternalId, s.HealthState,
s.LastModified, c.Name_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F,
c.IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F" )

After finding these am concatenating all these three like this

   devices = windows_devices.concat(unix_devices)
   @devices = devices.concat(network_devices)

I need to loop @devices in the view and i need to display the IP
Address for the all the devices.

When i do <%= device.IPAddress %> am getting error because the other
device uses the attribute
IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F .

I was struck with this issue. Please help me in this issue asap.

Catch the error and take appropriate action? Even better sort out the
database design.

Colin

Hi Colin,

  Thanks for the quick reply. I can't sort out the database design
becoz am pulling the data from SCOM sql server.
  Could you help me how can i solve this one please.

Please don't top post, it makes it difficult to follow the thread.
Insert your reply at appropriate point in previous message. Thanks.

Hi Colin,

Thanks for the quick reply. I can't sort out the database design
becoz am pulling the data from SCOM sql server.
Could you help me how can i solve this one please.

What about my other suggestion to catch the error?

Colin

Hi Colin,

I tried like this also <%= check_scom_column(device) %> in the view.
And in helper i provided like this

  def check_scom_column(device)
    if device.IPAddress
      return device.IPAddress
    elsif device.IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F
      return device.IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F
    end
  end

But it is giving me error undefined method `IPAddress' for
#<BaseManagedEntity:0x492d7d0>

Please help me Colin. I was struck with this issue from 2 days.

As I said, you could catch the error. Though probably respond_to?
would be better.

Colin

Eshwar Gouthama wrote in post #1016869:

Hi Everyone,

After finding these am concatenating all these three like this

   devices = windows_devices.concat(unix_devices)
   @devices = devices.concat(network_devices)

@devices = windows_devices + unix_devices + network_devices

I need to loop @devices in the view and i need to display the IP
Address for the all the devices.

When i do <%= device.IPAddress %> am getting error because the other
device uses the attribute
IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F .

I was struck with this issue. Please help me in this issue asap.

I tried like this also <%= check_scom_column(device) %> in the view.
And in helper i provided like this

  def check_scom_column(device)
    if device.IPAddress
      return device.IPAddress
    elsif device.IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F
      return device.IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F
    end
  end

But it is giving me error undefined method `IPAddress' for
#<BaseManagedEntity:0x492d7d0>

if device.respond_to?(:IPAddress)
  device.IPAdress
else
  device.IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F
end

Hi Colin,

  I didn't understand what you are saying. Could you tell me clearly
what should i do.

Hi Colin,

  Thanks a lot. I got the solution.

The first thing I was saying was not to top post, which you ignored.

Hi Colin,

I didn't understand what you are saying. Could you tell me clearly
what should i do.

> Hi Colin,

> I tried like this also <%= check_scom_column(device) %> in the view.
> And in helper i provided like this

> def check_scom_column(device)
> if device.IPAddress
> return device.IPAddress
> elsif device.IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F
> return device.IPAddress_65AC01F1_F20E_CE0D_42CA_B24D1DE49E5F
> end
> end

> But it is giving me error undefined method `IPAddress' for
> #<BaseManagedEntity:0x492d7d0>

As I said, you could catch the error. Though probably respond_to?
would be better.

What did you find when you googled for
ruby catch error
and
ruby respond_to

I see that 7stud has saved you that effort.

Perhaps working through some ruby and rails tutorials would be a good idea.

Colin