具有 has_one - belongs_to 关联的未定义方法
undefined method with has_one - belongs_to assocation
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :contract
belongs_to :customer
has_one :expense
end
class Expense < ActiveRecord::Base
belongs_to :activity
end
当我尝试在 ExpensesController 中创建一个 Expense
时,我得到 undefined method expense' for nil:NilClass
:
if !activity.try(:expense)
activity.expense.create(ref: @current_date, year: session[:current_year], month: session[:current_month],
days:["1", "2"])
end
我想这是正常的,因为服务器正在尝试从 nil
.
中调用 expense
问题是原来的代码不是我的。我必须添加 Activity
和 Expenses
之间的关联。我应该怎么做才能创建新的 Expenses
?
编辑:这是 ExpensesController
的 show
操作的完整代码
def show
if session[:current_month] == nil && session[:current_year] == nil
session[:current_month] = params[:month]
session[:current_year] = params[:year]
else
if session[:current_month] != params[:month] || session[:current_year] != params[:year]
session[:current_month] = params[:month]
session[:current_year] = params[:year]
end
end
@calendar, @previous_month, @next_month, @month_name = get_calendar(session[:current_year], session[:current_month])
@current_date = @month_name + " " + session[:current_year]
# If expense does not exist, create one with current month
activity = Activity.where(month: params[:month], year: params[:year]).first
if activity.try(:expense)
@nb_days = 0
@calendar.each do |c|
@nb_days += 1
end
else
activity.expense.create(ref: @current_date, year: session[:current_year], month: session[:current_month],
days:[""])
end
@expense = activity.expense
end
如果我没看错的话,你的 activity
变量是空集。我认为问题出在这里:
activity = Activity.where(month: params[:month], year: params[:year]).first
这没有返回任何记录。
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :contract
belongs_to :customer
has_one :expense
end
class Expense < ActiveRecord::Base
belongs_to :activity
end
当我尝试在 ExpensesController 中创建一个 Expense
时,我得到 undefined method expense' for nil:NilClass
:
if !activity.try(:expense)
activity.expense.create(ref: @current_date, year: session[:current_year], month: session[:current_month],
days:["1", "2"])
end
我想这是正常的,因为服务器正在尝试从 nil
.
expense
问题是原来的代码不是我的。我必须添加 Activity
和 Expenses
之间的关联。我应该怎么做才能创建新的 Expenses
?
编辑:这是 ExpensesController
show
操作的完整代码
def show
if session[:current_month] == nil && session[:current_year] == nil
session[:current_month] = params[:month]
session[:current_year] = params[:year]
else
if session[:current_month] != params[:month] || session[:current_year] != params[:year]
session[:current_month] = params[:month]
session[:current_year] = params[:year]
end
end
@calendar, @previous_month, @next_month, @month_name = get_calendar(session[:current_year], session[:current_month])
@current_date = @month_name + " " + session[:current_year]
# If expense does not exist, create one with current month
activity = Activity.where(month: params[:month], year: params[:year]).first
if activity.try(:expense)
@nb_days = 0
@calendar.each do |c|
@nb_days += 1
end
else
activity.expense.create(ref: @current_date, year: session[:current_year], month: session[:current_month],
days:[""])
end
@expense = activity.expense
end
如果我没看错的话,你的 activity
变量是空集。我认为问题出在这里:
activity = Activity.where(month: params[:month], year: params[:year]).first
这没有返回任何记录。