Can't figure this one out. In rails model, I want to call a method within the same model to manipulate data returned by a find method. This 'filter' method will be called from many custom find method within this model, so I want it to be separate. (and I cannot filter from the SQL it is too complicated)
Here is an example:
#controller
@data = Model.find_current
#model
class Model
def self.find_current
@rows = find(:all)
filter_my_rows
return @rows
end
def filter_my_rows
#do stuff here on @rows
for row in @rows
#basically I remove rows that do not meet certain conditions
end
end
end
The result of this is: undefined method `filter_my_rows'
Thank you for any help!
Part of the problem is you're defining a class method called find_current and an instance method called filter_my_rows. Generally you define them both within the same scope for them to work together.
Another thing is you can do a lot of the filtering you need with a simple Array#reject call. For example:
@models = all.reject do |m|
# This block is used to remove entries that do not qualify
# by having this evaluate to true.
!m.current
end
You can modularize this somewhat by plugging in functions as required, too, but that can get wildly complicated to manage if you're not careful.
# Define reusable blocks that are organized into a Hash
CONDITION_FILTERS = {
:current => lambda { |m| m.current }
}
# Array#select is the inverse of Array#reject
@models = all.select(CONDITION_FILTERS[:current])
While you stated in your question that this was only required because of concerns about not being able to determine the relevance of a particular record before all the records are loaded from the database, this is generally bad form since you will probably be rejecting a large amount of data that you've gone through the trouble of retrieving and instantiating as models only to immediately discard them.
If possible, you should at least cache the retrieved rows for the duration of the request so you don't have to keep fetching them over and over.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With