Rails3 对主动关系感到困惑
我已经查看了这方面的文档,但我仍然有点困惑。我的目标是返回 @mom 上的内容字段。但它因未定义的方法“内容”而失败。和@goals 有效。我对@mom 缺少什么?我怎样才能让它发挥作用?
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 %>
模型
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请在您的控制器中尝试此操作(它将返回一条记录,而不是包含一条记录的数组):
Try this in your controller instead (it'll return one record rather than an array with one record):