Search code examples
ruby-on-railsactive-relation

Rails3 confused on active relations


I have checkedout the docs on this but I am still a bit confused. My goal is to return the content field on @mom. But it fails with undefined method `content'. and @goals works. What am I missing about @mom and how can I get that to work?

project_controller.rb

def show
  @project = Project.find(params[:id])
  @goals = @project.projectgoals.find(:first, :order => "created_at DESC")
  @mom = @project.projectgoals.order(:created_at => "DESC").limit(1).all
end

Show.html.erb

<b>Name: </b><%= @project.name %><br/>
<b>Goals: </b><%= @goals.content %><br/>
<b>Goals: </b><%= @mom.content %>
<br/>
<%= debug @mom %>

Models

class Projectgoal < ActiveRecord::Base
  attr_accessible :content, :project_id
  belongs_to :projects
end

class Project < ActiveRecord::Base
  attr_accessible :name
  has_many :projectgoals
  has_many :projectstatuses
end

Solution

  • Try this in your controller instead (it'll return one record rather than an array with one record):

    @mom = @project.projectgoals.order("created_at DESC").first