开发者

undefined method when trying to access related records in model rails 3

开发者 https://www.devze.com 2023-02-15 13:08 出处:网络
What\'s wrong in my model, using rails 3.0.5 and ruby 1.9.2? class Milestone < ActiveRecord::Base has_many :capstone_milestones

What's wrong in my model, using rails 3.0.5 and ruby 1.9.2?

class Milestone < ActiveRecord::Base

  has_many :capstone_milestones
  has_many :capstones, :through => :capstone_milestones
  belongs_to :department
  attr_accessible :id, :name, :description, :department_id, :project

  accepts_nested_attributes_for :capstone_milestones, :allow_destroy => true

  def before_create # or after_initialize
    self.project ||= 'Default'
  end

  def xpos
    (Mileston开发者_开发百科e.department.id - 100000)*100
  end
end

When i do milestone.xpos in the view, i get "undefined method `department'" error message.

Thx!


You cant access department by class name because you will get it as an instance method. You can access like

@milestone = Milestone.find(id)
@milestone.department_id

In your case just replace Milestone with self.

(self.department.id - 100000)*100

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号