Question

I've seen the undefined local variable in many threads, also I have seen adding relationships a lot, so please read the problem before suggesting it's answered elsewhere. (of course, if you do find an answer elsewhere, I will be just as grateful)

The tables are Services and Checks (in the context of a garage, the service is the type and the checks are what need to be carried out for any particular service). This is many to many, so I've got a has many through relationship set up.

The problem is, although everything is working fine, I am still getting the error:

undefined local variable or method `check_ids'

Line:

@service.service_checks.build(check_id: check_ids, service_id: @service.id )

Any attempt I make to define the variable seems to stop the relationships from working. Which leaves me clueless as to how to get everything working in harmony.

Models:

class Service < ActiveRecord::Base
    has_many :service_checks, :dependent => :destroy
    has_many :checks, :through => :service_checks
    #attr_accessor :check_ids

    accepts_nested_attributes_for :checks
    accepts_nested_attributes_for :service_checks

end

class Check < ActiveRecord::Base
    has_many :service_checks
    has_many :services, :through => :service_checks
    belongs_to :check_cat
end

class ServiceCheck < ActiveRecord::Base
    belongs_to :service, dependent: :destroy
    belongs_to :check, dependent: :destroy

    accepts_nested_attributes_for :check, :reject_if => :all_blank
    self.primary_key = [:service_id, :check_id]
end

My update function in Service looks like this:

      def update

        respond_to do |format|
          if @service.update(service_params)

              #  This builds the relationships
                  @service.service_checks.build(check_id: check_ids, service_id: @service.id )



            format.html { redirect_to @service, notice: 'Service was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render action: 'edit' }
            format.json { render json: @service.errors, status: :unprocessable_entity }
          end
        end
  end


    # Never trust parameters from the scary internet, only allow the white list through.
    def service_params
params.require(:service).permit(:name, :price, :check_ids => [])
    end

And my form is simply:

<%= f.association :checks,
as: :check_boxes,
label_method: :name,
value_method: :id,
label: 'Checks' %>
Was it helpful?

Solution

I think you've got everything setup for this to work OK if you just remove the line for @service.service_checks.build(check_id: check_ids, service_id: @service.id ). The association build is already handled. And once you're past update everything has been saved already.

The reason you're getting an undefined variable error is because check_ids doesn't exist. You are in a context where the params array coming down from the form and the check ids are available in params[:service][:check_ids] if you needed to access them. Note also that if you had to call service_checks.build you would want to pass a single value to the check_id attribute, and not an array like params[:service][:check_ids].

But, again, I think what you really want to try is removing the build line.

OTHER TIPS

It would appear I do not need this line at all.

@service.service_checks.build(check_id: check_ids, service_id: @service.id )

Due to the relationship defined in the model, Rails is magically able to do the rest automatically!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top