이준호
이준호

Reputation: 25

Array in Ruby on rails, undefined method `minimum'

@click = Missing.select{|d| d.click < 10000}

find = @click.minimum(:id)
@data = Missing.where(id: find)
dataSize = @data.size
@renderData = @data[dataSize - 1]
render :json => @renderData

Errors:

undefined method `minimum' for #<Array:0x00000004acd678>

Error code : find = @click.minimum(:id)

I have no idea why it is wrong.

render :json = @click is working. find = Missing.minimum(:id) is working.

Upvotes: 0

Views: 465

Answers (2)

joshweir
joshweir

Reputation: 5617

@click = Missing.select{|d| d.click < 10000} is returning an array of active record instances. This is why minimum can not be called against an array.

If you want the first record in the @click set then use @click.first.id. If you want the smallest (min) id then use @click.map(&:id).min. Looking at your code i'm not sure what you are trying to achieve?

On a side note, i'd avoid calling Missing.select{|d| d.click < 10000} because that is doing a full table scan on your Missing model's table. As Ruby Racer says, better to query with active record.

Upvotes: 1

Ruby Racer
Ruby Racer

Reputation: 5740

@click is an array, not an association.

Why don't you use activerecord query to get your find?

find = Missing.where('click < 10000').minimum(:id)

Upvotes: 0

Related Questions