I´m trying to make a User – Infos association where my User has_many Infos.
I´m trying to call my User Infos on the index form from Users.
By calling <%= user.infos %> on my index.html.erb (Users) it returns me all datas from the Infos table like this:
[#<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">]
I assumed that by doing <%= user.infos.comment %> it would return me the “comment” from that user, but all I get is this error:
undefined method `comment' for #<ActiveRecord::Relation:0x3b8ebe8>
What am I doing wrong?
infos_controller:
class InfosController < 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
info model:
class Info < ActiveRecord::Base
belongs_to :user
end
user model:
class User < ActiveRecord::Base
validates :login, :presence => true
validates :password, :presence => true,
:length => {:minimum => 5}
has_many :infos, :dependent => :destroy
end
This:
is an array. You have to choose one of the objects. You can’t call an attribute value without selecting one of theses objects. Okay, in this case there is only one object but that doesn’t matter.