{"action":"created","issue":{"url":"https://api.github.com/repos/sferik/rails_admin/issues/2379","labels_url":"https://api.github.com/repos/sferik/rails_admin/issues/2379/labels{/name}","comments_url":"https://api.github.com/repos/sferik/rails_admin/issues/2379/comments","events_url":"https://api.github.com/repos/sferik/rails_admin/issues/2379/events","html_url":"https://github.com/sferik/rails_admin/pull/2379","id":100844124,"number":2379,"title":"Add ability to view has_many associations","user":{"login":"corbt","id":176426,"avatar_url":"https://avatars.githubusercontent.com/u/176426?v=3","gravatar_id":"","url":"https://api.github.com/users/corbt","html_url":"https://github.com/corbt","followers_url":"https://api.github.com/users/corbt/followers","following_url":"https://api.github.com/users/corbt/following{/other_user}","gists_url":"https://api.github.com/users/corbt/gists{/gist_id}","starred_url":"https://api.github.com/users/corbt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/corbt/subscriptions","organizations_url":"https://api.github.com/users/corbt/orgs","repos_url":"https://api.github.com/users/corbt/repos","events_url":"https://api.github.com/users/corbt/events{/privacy}","received_events_url":"https://api.github.com/users/corbt/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-08-13T19:00:32Z","updated_at":"2015-08-22T01:02:33Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/sferik/rails_admin/pulls/2379","html_url":"https://github.com/sferik/rails_admin/pull/2379","diff_url":"https://github.com/sferik/rails_admin/pull/2379.diff","patch_url":"https://github.com/sferik/rails_admin/pull/2379.patch"},"body":"I've found it generally useful to be able to view all the `has_many` associations associated with an object.\r\n\r\nAs a concrete example, in the app I'm working on a user can take video recordings, which are associated with their user account through a `has_many` association. Video files take a while to process after being uploaded, and it's helpful to be able to see the state of all of a user's recordings. Unfortunately, the current default implementation of rails_admin's `association` field type only shows the name of each association, and to find out more details you'd need to click on every associated object. Since in my case a user may have dozens of recordings, this becomes tedious quickly.\r\n\r\nThis pull request introduces a new `view_url` instance option that, when present in a rails_admin field, adds a link in the field's header on the show page. Additionally, I've created an implementation of this method for the `association` field type that, when more than one associated object is present (and the associated object is registered with rails_admin), leads to a filtered index page for the associated model showing the objects associated with the object in question.\r\n\r\nIf that didn't make sense, hopefully these screen shots for my `User` and `Recording` class make it clear how this works (the \"View\" link from the first image leads to the index page seen in the second).\r\n\r\n<img width=\"1056\" alt=\"screen shot 2015-08-13 at 7 50 10 pm\" src=\"https://cloud.githubusercontent.com/assets/176426/9258853/b506a88c-41f5-11e5-8356-9729b885a949.png\">\r\n\r\n<img width=\"1054\" alt=\"screen shot 2015-08-13 at 7 50 37 pm\" src=\"https://cloud.githubusercontent.com/assets/176426/9258855/bb27fd6a-41f5-11e5-9079-bdf3f01bc502.png\">"},"comment":{"url":"https://api.github.com/repos/sferik/rails_admin/issues/comments/133621222","html_url":"https://github.com/sferik/rails_admin/pull/2379#issuecomment-133621222","issue_url":"https://api.github.com/repos/sferik/rails_admin/issues/2379","id":133621222,"user":{"login":"fmh","id":56861,"avatar_url":"https://avatars.githubusercontent.com/u/56861?v=3","gravatar_id":"","url":"https://api.github.com/users/fmh","html_url":"https://github.com/fmh","followers_url":"https://api.github.com/users/fmh/followers","following_url":"https://api.github.com/users/fmh/following{/other_user}","gists_url":"https://api.github.com/users/fmh/gists{/gist_id}","starred_url":"https://api.github.com/users/fmh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fmh/subscriptions","organizations_url":"https://api.github.com/users/fmh/orgs","repos_url":"https://api.github.com/users/fmh/repos","events_url":"https://api.github.com/users/fmh/events{/privacy}","received_events_url":"https://api.github.com/users/fmh/received_events","type":"User","site_admin":false},"created_at":"2015-08-22T01:02:33Z","updated_at":"2015-08-22T01:02:33Z","body":"I had the same need, but I proceeded otherwise.\r\n1- create \"_has_many_details_html.html\" in rails_admin/main\r\n``````ruby\r\n:ruby\r\n  require 'rails_admin/config/fields/types/datetime.rb'\r\n\r\n  @model_config = model_config\r\n  @abstract_model = abstract_model\r\n  @objects = objects\r\n\r\n  params = request.params.except(:authenticity_token, :action, :controller, :utf8, :bulk_export, :_pjax)\r\n\r\n  params.delete(:sort_reverse) unless params[:sort_reverse] == 'true'\r\n\r\n  params.delete(:sort) if params[:sort] == @model_config.list.sort_by.to_s\r\n\r\n  properties = @model_config.list.with(controller: self.controller, view: self, object: @abstract_model.model.new).visible_fields\r\n  # columns paginate\r\n  @filterable_fields = @model_config.list.fields.select(&:filterable?)\r\n  sets = get_column_sets(properties)\r\n  properties = sets[params[:set].to_i] || []\r\n\r\n\r\n#list\r\n  %style\r\n\r\n    - properties.select{ |p| p.column_width.present? }.each do |property|\r\n      = \"#list th.#{property.css_class} { width: #{property.column_width}px; min-width: #{property.column_width}px; }\"\r\n      = \"#list td.#{property.css_class} { max-width: #{property.column_width}px; }\"\r\n\r\n\r\n  %table.table.table-condensed.table-striped.table-hover\r\n    %thead\r\n      %tr\r\n        %th.shrink\r\n        - properties.each do |property|\r\n          %th= capitalize_first_letter(property.label)\r\n\r\n    %tbody\r\n      - @objects.each do |object|\r\n        %tr{class: \"#{@abstract_model.param_key}_row\"}\r\n          %td\r\n            = link_to \"<i class='btn btn-primary fa fa-info-circle'></i>\".html_safe , show_path(model_name:abstract_model,id:object.id) , class:'pjax'\r\n            = link_to \"<i class='btn btn-info fa fa-pencil'></i>\".html_safe , edit_path(model_name:abstract_model,id:object.id), class:'pjax'\r\n          - properties.map{ |property| property.bind(:object, object) }.each do |property|\r\n            - value =  property.pretty_value\r\n            %td{class: \"#{property.css_class} #{property.type_css_class}\", title: strip_tags(value.to_s)}= value\r\n`````\r\n2- some code in show.html.haml , replace line 16\r\n````ruby\r\n - if field.type == :has_many_association\r\n                    = render partial: 'has_many_details', locals: {objects: @object.send(field.name) , model_config: field.associated_model_config , abstract_model: field.associated_model_config.abstract_model}\r\n                  - else\r\n                    = field.type == :ck_editor ? field.pretty_value.html_safe : field.pretty_value\r\n````\r\nResult : \r\n\r\n![capture d ecran 2015-08-22 a 02 55 37](https://cloud.githubusercontent.com/assets/56861/9421770/29e652b2-487a-11e5-8d99-82f7007e7cc1.png)\r\n\r\n"}}