Reputation: 105
I'm having an issue pulling the correct information into a dashboard column using ActiveAdmin. If anyone could help me wrap my head around this issue it would be greatly appreciated.
The issue I'm having is retaining the data from an associated class with the current record I'm displaying (line 6 below).
Here is the column I've created:
column do
panel "Recent Beacons" do
table_for Beacon.limit(20) do
column("Mac address") { |b| link_to b.mac_address, admin_beacon_path(b) }
column "Last seen", :updated_at
column("House") { |b| link_to b.root_house, admin_house_path(b.root_house.id) }
column("Status") { |b| b.online? ? status_tag('Online', :green) : status_tag('Offline', :red) }
end
end
end
Using binding.pry I'm able to perform a b.meters.pop.circuit.root
and return the correct value I'm looking for. However it ActiveAdmin doesn't like this and the page errors with the following undefined method circuit for nil:NilClass
. Trying to clean up the code a bit I wrote a root_house
method.
def root_house
meters.pop.circuit.root
end
ActiveAdmin has left me a bit confused and any help is greatly appreciated.
Thanks!
Upvotes: 0
Views: 62
Reputation: 4116
Here's a dirty fix for this, refactor as you wish:
def root_house
if meters && meters.pop && meters.pop.circuit
meters.pop.circuit.root
else
'object is not present'
end
end
So I'm only going to call meters.pop.circuit.root
if meters
and meters.pop
and meters.pop.ciruit
is not nil.
Google null object pattern and law of Demeter ruby for a cleaner solution, also you can use delegate
method, this might be helpful.
Upvotes: 1