Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display associated model's attribute in Active Admin index with belongs_to/has_many relationship (Rails 3.2/Active Admin)
    primarykey
    data
    text
    <p>I'm building a daily deal Rails app to learn RoR.</p> <p>I am facing a problem for the past few hours : i can't get a model's attribute of an other associated model on active admin. Let me show you exactly the problem :</p> <p>I have two models: Brand (i.e the brand of the deal) and Deal. A deal belongs to a Brand but a Brand can have many Deals.</p> <p>models/deal.rb is like this:</p> <pre><code>class Deal &lt; ActiveRecord::Base belongs_to :brand </code></pre> <p>and we have models/brand.rb:</p> <pre><code>class Brand &lt; ActiveRecord::Base has_many :deals attr_accessible :name </code></pre> <p>And i did the t.belongs_to in my migrations so this is ok.</p> <p>In Active Admin's Deals' create form , i type, as admin, which brand the deal is associated with:</p> <p>admin/game.rb</p> <pre><code>ActiveAdmin.register Deal do # -- Form ----------------------------------------------------------- form do |f| f.inputs "Brand (i.e. client)" do f.input :brand_id, :label =&gt; "Select a brand:", :as =&gt; :select, :collection =&gt; Brand.all end </code></pre> <p>it works great, and i can create Deals with a certain brand. but <strong>I CAN'T manage to display the NAME of the Brand in my list of Deals</strong>:</p> <pre><code>ActiveAdmin.register Deal do index do selectable_column # id_column column :title column :deal_amount column :brand do |deal| link_to deal.brand.name end </code></pre> <p>...doesn't work.</p> <p>How can I do that ?</p> <p>I tried everything but <strong>i basically don't know how to fetch the name of a Brand given it matches the brand_id in the Deal's table</strong>.</p> <p>Any help appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload