【问题标题】:Rails3 confused on active relationsRails3 对主动关系感到困惑
【发布时间】:2011-06-10 00:31:07
【问题描述】:

我已经检查了这方面的文档,但我仍然有点困惑。我的目标是返回@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

显示.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

【问题讨论】:

    标签: ruby-on-rails active-relation


    【解决方案1】:

    在你的控制器中试试这个(它会返回一个记录而不是一个包含一个记录的数组):

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

    【讨论】:

    • 叹息...这是漫长的一天。谢谢!
    猜你喜欢
    • 2018-03-05
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2015-11-29
    • 2015-05-09
    • 2019-11-19
    • 2017-10-10
    相关资源
    最近更新 更多