[英]Rails3 confused on active relations
我已經檢查了這方面的文檔,但我仍然有點困惑。 我的目標是返回@mom 上的內容字段。 但它因未定義的方法“內容”而失敗。 @goals 有效。 我對@mom 缺少什么,我怎樣才能讓它發揮作用?
項目控制器.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
請在您的 controller 中嘗試此操作(它將返回一條記錄,而不是返回一條記錄的數組):
@mom = @project.projectgoals.order("created_at DESC").first
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.