0

Im trying to create a new alert only if is covered returns true, how can I access the object from new to check if it is covered or not, and if it is save correctly if not respond with the errors. My error is NameError (undefined local variable or method `params' for #):

  class Alert < ActiveRecord::Base
    belongs_to :user
    has_many :activities

    scope :by_year, lambda { |year| where('extract(year from created_at) = ?', year) }
    validate :is_covered, :before => :create

    def is_covered

        @schools = School.all

    @schools.each do |school|

            distance = triangulate(school.latitude, school.longitude, self.latitude, self.longitude)

      if distance < school.radius.to_f  
        self.school_id = school.id
        return self
      else
        self.errors.add(:latitude,"Sorry You Arent Within a School")
        return self
      end
    end
    end
    def power(num, pow)
        num ** pow
  end
    def triangulate(lat1, lng1, lat2, lng2)

    dtor = Math::PI/180
    r = 6378.14*1000

        rlat1 = lat1.to_f * dtor 
    rlong1 = lng1.to_f * dtor 
    rlat2 = lat2.to_f * dtor 
    rlong2 = lng2.to_f * dtor
    dlon = rlong1 - rlong2
    dlat = rlat1 - rlat2 

        a = power(Math::sin(dlat/2), 2) + Math::cos(rlat1) * Math::cos(rlat2) * power(Math::sin(dlon/2), 2)
        c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a))

        distance = r * c

    return distance
  end
end

Controller

   def create
        @alert = Alert.create(alert_params).is_covered
        @alert.status = "active"

        respond_to do |format|
            if @alert.save && @alert.valid?
              log_activity(nil, "has created alert", @alert.id, @alert.school_id, "Alert")
              format.json { render json: @alert.id}
            else
              format.json { render json: @alert.errors, status: :unprocessable_entity }
            end
        end
      end

1 Answer 1

2

You can call the method in your create action in your controller, it should look something like:

def create
  if Alert.new.is_covered?
    #... Do some stuff
  end
end

Also given that you're not passing any argument from your controller to the Alert Class and then to that method, it probably won't do what you're trying to do, but that's another issue.

I assumed your question is more along the lines of how to access that method from the controller.

Sign up to request clarification or add additional context in comments.

5 Comments

if i do use that how can i get the alert object
Yes you can use self inside the method. But you can exclude the self. For example: if you wanted activities you'd call self.activities, that's the same as calling activities.
NameError (undefined local variable or method `params' for #<Alert:0x007fe5993593d8>): thats what i get
I don't see the changes you made to your Alert class.
it should be something like that... def create if Alert.new.is_covered? @alert = Alert.create(alert_params) #... remaining of the method goes here end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.