6
votes

I currently have a Rails 3.0.10 app and I made two models with simple associations: has_many, and belongs_to.

After installing the ActiveAdmin plugin and creating the corresponding Ruby resource files, I've noticed some strange behavior.

Below, you can see that "Job File" belongs_to "Ernet Clients". The associations are all working correctly, however, the display name is appearing as the actual ActiveRecord object instead of the client name.

enter image description here

This is the show view when under the "Job File" section:

enter image description here

But, if I go to view the actual client, the correct text appears:

enter image description here

There must be something going wrong in the "Job File" resource that is causing this, but I can't figure out what it could be. After googling I found this: http://groups.google.com/group/activeadmin/browse_thread/thread/2a261e070efa7bae

Within the JobFile.register file I specified the display name with this:

filter :ernet_client, :display_name_methods => :display_name

This didn't work, though, and I cycled through all of the other available names to no avail.

I looked up ActiveAdmin's dependencies and sass-rails seems to be the only one - but that is if you are using 3.1 and I'm using Rails 3.0.10.

And finally, to be sure that my associations were indeed working, I opened up the Rails console and created a job file:

job = JobFile.new
=> #<JobFile hash returned>
job.ernet_client_id = 2
=> 2
job.ernet_client.client_name
=> Target

Everything seems to be working as it should.

Anyone have any insight on to how to solve this?

2

2 Answers

10
votes

Did you try defining a to_s method on the ErnetClient model?

def to_s
  display_name
end

It looks like this is the method that is being called automatically, as if you did call to_s on one of these objects in the console you would get a similar result.

5
votes

You can find the explanation here Filter select show object instead of object name.

# Active Admin makes educated guesses when displaying objects, 
this is the list of methods it tries calling in order
        setting :display_name_methods, [ :display_name,
                                          :full_name,
                                          :name,
                                          :username,
                                          :login,
                                          :title,
                                          :email,
                                          :to_s ]

You may define a display_name, full_name, or ... (see the list above) ... method in your model. ActiveAdmin looks for one of those.