0

I am trying to search through my model using 3 columns. Also if the column is empty, it is valid. This is how I am doing it

def getactivityfortoday
@temp = params[:temp]
logger.debug "params temp:#{@temp.inspect}"
@sky = params[:sky]
@day = params[:day]
@todaysactivities = []
@activities=[]
@finaldata = []
@activities = Weatherclockactivity.all
@attemptactivities = []
@attemptactivities = @user.attempts
for activity in @activities do
  logger.debug "activity: #{activity.attributes.inspect}"
  if @temp.to_i < activity.temperatureMax.to_i && @temp.to_i > activity.temperatuureMin.to_i
    if @sky == activity.sky || activity.sky == ""
      if @day == activity.day 
        @todaysactivities << activity
      end
    end
  end
end
for activity in @todaysactivities
  for attempt in @attemptactivities
    if attempt == activity
      finaldata << {activity: activity, attempt: "yes"}
    else
      finaldata << {activity: activity, attempt: "no"}
    end
  end
end

respond_to do |format|  
    format.html { render action: "new" }
    format.json { render json: @finaldata }
end

The response I get is an empty array but I should be getting 3 rows as a response.

3 Answers 3

1

spelling mistake here

activity.temperatuureMin.to_i  

And

finaldata << {activity: activity, attempt: "yes"}

should be

@finaldata << {activity: activity, attempt: "yes"}

Also you could be more concise

def getactivityfortoday
  @temp = params[:temp]
  logger.debug "params temp:#{@temp.inspect}"
  @sky = params[:sky]
  @day = params[:day]
  @activities = Weatherclockactivity.all
  @attemptactivities = @user.attempts
  @finaldata = @activities.map do |activity|
    if (activity.temperatureMin.to_i + 1...activity.temperatureMax.to_i).include?(@temp.to_i)  && ( @sky == activity.sky || activity.sky == "") && @day
       @attemptactivities.include?(activity) ? {activity: activity, attempt: "yes"} : {activity: activity, attempt: "no"}  
    end 
  end.compact
  respond_to do |format|  
    format.html { render action: "new" }
    format.json { render json: @finaldata }
  end
end
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for pointing the typo, I corrected it and it kind off worked. I studied your code, tried to implement it and I get null objects back. No idea why. I should be getting 3 activities but I get 5(all activities) null objects
It seems like the last 2 conditions are somehow causing this. Maybe it is worthy to note that day is a boolean
is @user.attempts a collection of Weatherclockactivity Objects? Your code suggests it should be but this could be causing null. I edited the code a bit but not sure why you are not getting anything back.
Can you post your params because if @temp, @sky, Or @day = nil then nothing will return.
0

How about something like this?

I tried to make it a balance of readability and conciseness. First we filter for the desired activities. Then we structure the output. This should be easier to debug.

def getactivityfortoday
  @temp = params[:temp].to_i
  @sky = params[:sky]
  @day = params[:day]

  @activities = Weatherclockactivity.all

  @attemptactivities = @user.attempts

  selected_activities = @activities.select do |activity|
    # Make sure it's the right temperaure
    return false unless (activity.temperatureMin.to_i + 1 ... activity.temperatureMax.to_i).include? @temp

    # Make sure the sky matches, or the sky is blank
    return false unless (@sky.blank? || @sky.activity == activity.sky)

    # Make sure the day matches
    return false unless @day == activity.day

    # Otherwise, it's good!
    return true
  end

  selected_attempted_activities = selected_activities.map do|activity| 
    ret = {activity: activity}
    ret[:attempt] = @attemptactivities.include?(activity) ? "yes" : "no"
    ret
  end

  respond_to do |format|  
    format.html { render action: "new" }
    format.json { render json: selected_attempted_activities }
  end
end

There are a few typos in your original (for instance, @finaldata not finaldata). Make sure that you spell instance variables (things starting with @, like @sky) correctly, since if you try to access an undefined instance variable, it'll silently default to nil.

Comments

0

The best and flexible way is to use ActiveModel::Model It allows you to use many more useful methods. it will seems like: app/models/activity_report.rb

Class ActivityReport
  include ActiveModel::Model

  attr_accessor :day, :activity # and etc.

  validates :day, presence: true

  def day
    @day.to_s # for example
  end

  def day=(value)
    @day = value - 1.month # for example every date which user set will set on one month ago
  end

  # and etc
end

app/controllers/posts_controller.rb

...
def index
  @activity = ActivityReport.new(params[:activity])
end

def create
  @activity.create!
end
...

app/views/posts/index.html.haml

= form_for @activity do |f|
  = f.day

For more information you could take a look at: http://edgeapi.rubyonrails.org/classes/ActiveModel/Model.html
http://railscasts.com/episodes/219-active-model (old)
http://railscasts.com/episodes/416-form-objects (newer, but a little complex)

Comments

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.