ActiveRecord array/collection manage

Hello, I have a model like this:

class Car < ActiveRecord::Base
def color©
where(“color = ?”,c)
end
end

Suppose that I want to show the red and blue cars. I could do
something like this:
@cars = Car.color(‘blue’) + Car.color(‘red’)

The problem I’m finding is that now, @cars is an array and I could not
do something like that:
@cars.page(params[:page]).per(10)
@cars.order( … )
or any method ActiveRecord implements. The question is: How can I
manage ActiveRecord array/collection for do: + - | &?

Thanks very much.

Should it be:

class Car < ActiveRecord::Base
def self.color©

end
end

Allan

Pagination obviously doesn’t works on Array, but ActiveRecord::Relation
set.

You have to rewrite your color selector using ‘standard de facto’
approach:

class Car < ActiveRecord::Base
scope :colored, lambda { |color| where(:color => color) }
end

Then just call:

Car.colored(%w{red blue}).page … etc

Great Valery! I’ve solved it.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs