Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails - getting association data
    primarykey
    data
    text
    <p>I´m trying to make a User - Infos association where my User has_many Infos.</p> <p>I´m trying to call my User Infos on the index form from Users.</p> <p>By calling <code>&lt;%= user.infos %&gt;</code> on my index.html.erb (Users) it returns me all datas from the Infos table like this:</p> <pre><code>[#&lt;Info id: 2, name: "Thales Miguel", date: "1989-07-14", area: "An\xC3\xA1lise de Sistemas", comment: "Analista j\xC3\xBAnior de sistemas.", user_id: 1, created_at: "2012-01-16 15:54:29", updated_at: "2012-01-16 15:54:29"&gt;] </code></pre> <p>I assumed that by doing <code>&lt;%= user.infos.comment %&gt;</code> it would return me the "comment" from that user, but all I get is this error:</p> <pre><code>undefined method `comment' for #&lt;ActiveRecord::Relation:0x3b8ebe8&gt; </code></pre> <p>What am I doing wrong?</p> <p>infos_controller:</p> <pre><code>class InfosController &lt; ApplicationController def create @user = User.find(params[:user_id]) @info = @user.infos.create(params[:info]) redirect_to user_path(@user) end def destroy @user = User.find(params[:user_id]) @info = @user.infos.find(params[:id]) @info.destroy redirect_to user_path(@user) end def new @user = User.new @user.build_info end end </code></pre> <p>info model:</p> <pre><code>class Info &lt; ActiveRecord::Base belongs_to :user end </code></pre> <p>user model:</p> <pre><code>class User &lt; ActiveRecord::Base validates :login, :presence =&gt; true validates :password, :presence =&gt; true, :length =&gt; {:minimum =&gt; 5} has_many :infos, :dependent =&gt; :destroy end </code></pre>
    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.
    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