Rails: How to make available parent attribute in setter method

試著忘記壹切 提交于 2019-12-12 09:52:15

问题


Context:

I have a company model with many projects, having many tasks. The company also has many employees, which in turn have many tasks.

Schema:

Problem:

I'm building a form to create a project where the user can add multiple tasks. Upon submission the form should create a single project record, one or more task records with hours and employee_id attributes and (!) check if the employee name already exists in the database or create a new one.

I've approached this by adding jquery autocomplete to the form and defining a virtual attribute with a getter and setter method in my Task model. Similar to Railscast #102.

This succesfully sets the employee_id in tasks and creates a new employee record with employee name and task_id.

The problem is currently that the model does not save a company_id.

My code:

class Task < ActiveRecord::Base
    belongs_to :project
    belongs_to :employee

    def employee_name
      employee.try(:name)
    end

    def employee_name=(name)
      self.employee = Employee.find_or_create_by(name: name) if name.present?
    end
end

Question:

How can I make the company_id attribute of the parent model available the setter method of my Task model? Like so:

def shareholder_name=(name)
    self.shareholder = Shareholder.find_or_create_by(name: name, company_id: company_id) if name.present?
end

At the moment this yields the error:

undefined local variable or method "company_id" for #<Task:0x98438b8>

EDIT:

Or, if I try project.company_id this tells me project is a NilClass:

undefined method company_id for nil:NilClass

UPDATE:

Still unsolved. Any ideas why project is nil here?


回答1:


class Task < ActiveRecord::Base
  belongs_to :project
  belongs_to :employee

  def shareholder_name=(name)
    self.shareholder = Shareholder.find_or_create_by(name: name, company_id: project.company_id) if name.present?
  end
end 

also, so typical pattern with rails, for instance where you have belongs_to :project, that would add an instance method called project to Task, that instance method could then be used to get project associated with task and subsequently the associated company_id in case that may be of interest



来源:https://stackoverflow.com/questions/35494349/rails-how-to-make-available-parent-attribute-in-setter-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!