[英]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.
我的目标是返回@mom 上的内容字段。 But it fails with undefined method `content'.
但它因未定义的方法“内容”而失败。 and @goals works.
@goals 有效。 What am I missing about @mom and how can I get that to work?
我对@mom 缺少什么,我怎样才能让它发挥作用?
project_controller.rb项目控制器.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显示.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):请在您的 controller 中尝试此操作(它将返回一条记录,而不是返回一条记录的数组):
@mom = @project.projectgoals.order("created_at DESC").first
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.