简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM