Displaying other model attributes in the list view of rails_admin

4,245 views
Skip to first unread message

Lee Graham

unread,
May 13, 2013, 2:42:00 PM5/13/13
to rails...@googlegroups.com

I realize that the purpose of rails_admin is supposed to essentially be a model admin tool, but I would like to have a list view for a model and include attributes of associated models in the list of the of model.

For example. I have a scale model and a container model. The container has an attribute 'weight', and the scale is associated via a 'has_one' relationship to container.

How can I include scale.container.weight in the scale model list view?

And example of my current Scale model is:

class Scale < ActiveRecord::Base
   attr_accessible :activated, :cluster, :name, :plant, :status, :timer, :unauthorized, :uid, :container_id, :customer_id

    has_one :container

    rails_admin do
       list do
          field :name
          field :activated
          field Scale.container.weight # This is what I'm trying to include. However, this certainly doesn't produce the desired results. 
       end
    end
end

Thanks!

Benoit Bénézech

unread,
May 16, 2013, 4:19:18 AM5/16/13
to rails...@googlegroups.com
Use 'accepts_nested_attributes_for :container' in the Scale model and configure the nested section of the Container model to show only the relevant column.

Otherwise you can use a delegate or your own getter/setter and create a column with the same name. Just mark it as 'searchable false/sortable false'.

Lee Graham

unread,
May 16, 2013, 12:18:53 PM5/16/13
to rails...@googlegroups.com
Hi Benoit, 

How exactly would I configure the nested section of container? I've tried something like: 

Container.rb

class Container < ActiveRecord::Base
   attr_accessible :name, :material_type, :maxTimestamp, :eightyTimestamp, :tare_weight, :weight, :weight_type, :unit, :scale_id, :alloy, :material, :customer, :max_weight, :price, :rfid_id, :customer_id

   belongs_to :scale
   belongs_to :customer
   has_one :rfid

   validates_presence_of :scale, :customer

   rails_admin do
      configure :nested_form_for
         field :weight do
         end
   end
end

So, the weight only shows up when I go to the container, but when I navigate to the scale, it doesn't show the nested attribute.

scale.rb

class Scale < ActiveRecord::Base
   attr_accessible :activated, :cluster, :name, :plant, :status, :timer, :unauthorized, :uid, :container_id, :customer_id, # :as => :admin
   # attr_accessible :activated

   # Make the Scale model reportable to Ruport
   acts_as_reportable
   
   has_one :container

   # Paper_trail privides history tracking for models
   # For documentation on how to use paper_trail, see:
   # has_paper_trail

   # Configures the list, details, etc. views in rails_admin. For 
   # details on how to customize this configuration please see:

   accepts_nested_attributes_for :container

   rails_admin do
      list do
         field :name
         field :activated
         field :weight # it doesn't show weight here as a nested attribute
      end
      edit do
      end
   end
end

Thanks, 

Lee

Lee Graham

unread,
May 17, 2013, 12:03:37 PM5/17/13
to rails...@googlegroups.com
I see that I also need to include container_attributes since I am using attr_accesible. My container now looks like:

Scale.rb
------------

class Scale < ActiveRecord::Base
   attr_accessible :activated, :cluster, :name, :plant, :status, :timer, 
   :unauthorized, :uid, :container_id, :customer_id, :container_attributes, :battery_id
   
   has_one :container

   accepts_nested_attributes_for :container

   rails_admin do
       list do
          field :name
          field :activated
          field :container # when I add this, I get the link to the container model list view
        end
   end
end

Container.rb
-----------------

class Container < ActiveRecord::Base
   attr_accessible :name, :material_type, :maxTimestamp, :eightyTimestamp, :tare_weight, 
   :weight, :weight_type, :unit, :scale_id, :alloy, :material, :customer, :max_weight, 
   :price, :rfid_id, :customer_id
 
   belongs_to :scale

   validates_presence_of :scale

   rails_admin do
      list do
         field :weight
      end
      show do
         field :weight
      end
   end
end

The problem is that I don't understand how to use the configure option in the DSL to just display the weight on Scale list view. The closest I seem to be able to get to display the weight of the container on the list view is I can get a link to the model to appear by adding `field :container` in the rails_admin config for scale. I know you mentioned using `configure` to add the various attributes of the nested model, but I'm not clear on how to use it.

Thanks so much for your assistance! 

Joop Verbaken

unread,
May 17, 2013, 12:32:09 PM5/17/13
to rails...@googlegroups.com
Hi Lee

I am also struggling with this and have not resolved it.
If you resolve it, your code could be the example that we all can look for to implement this great feature in rails_admin.

I hope too that some one will help us out.

Thanks already for writing this up.

Joop 


--
You received this message because you are subscribed to the Google Groups "rails_admin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rails_admin...@googlegroups.com.
To post to this group, send email to rails...@googlegroups.com.
Visit this group at http://groups.google.com/group/rails_admin?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Lee Graham

unread,
May 17, 2013, 3:08:40 PM5/17/13
to rails...@googlegroups.com
Hi Joop, 

Glad to hear that I'm not the only one. Let's see what the rails_admin master Benoit has to say :)

Lee

Benoit Bénézech

unread,
Jun 18, 2013, 5:28:49 AM6/18/13
to rails...@googlegroups.com
With delegates :

class Scale < ActiveRecord::Base
    has_one :container
    delegate :weight, to: :container, prefix: true

    rails_admin do
       list do
          field :name
          field :
activated
          field :container_weight do
             searchable false # AR sorting/searching probably don't work out of the box with delegates, I'm not an expert on this
          end
       end
    end
end

Or RailsAdmin only:

class Scale < ActiveRecord::Base
    has_one :container
    rails_admin do
       list do
          field :name
          field :
activated
          field :container_weight do
             def value
               bindings[:object].container.weight
             end
          end
       end
    end
end

For forms, you will need to go with accepts_nested_attributes. (peruse through the wiki for more)

Good luck guys !

Куликов Дмитрий

unread,
Jul 10, 2018, 5:09:37 PM7/10/18
to rails_admin
Hello! Try this:

config.model Model do
    list do
      field :assos_field do
        pretty_value do
          bindings[:object].assos_field.your_field
        end
      end
      include_all_fields
    end
  end

Ricardo Paiva

unread,
Apr 9, 2019, 6:33:20 PM4/9/19
to rails_admin
is it possible to hide a column based on a user role (by accessing devise current_user) ?
thanks
Reply all
Reply to author
Forward
0 new messages