views:

974

answers:

3

Hello all.

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!

+2  A: 

use a named_scope instead

named_scope :current, :conditions => {:active => true} # this is normal find criteria

then in your controller

@date = Model.current

you can also make the named_scopes lambda functions

ErsatzRyan
Thanks, but I (think) can't use scope or any condition filtering for the SQL because the conditions that I need to check comes from data from different databases (on different servers). SO I have to loop them and check them one by one trought the belongs_to association itself.
mickey
A: 

What's wrong with your solutions? What are you looking for exactly? If I understood your point of view, the main problem of your implementation is that

This 'filter' method will be called from many custom find method within this model, so I want it to be separate.

... that you can't use named_scopes or with_scope, the first solution that comes to my mind is to create a custom wrapper to act as a filter.

class Model
  def self.find_current
    filtered do
      all
    end
  end

  def self.other_method
    filtered do
      all :conditions => { :foo => "bar" }
    end
  end

  def self.filtered(&block)
    records = yield
    # do something with records
    records
  end

end
Simone Carletti
What's wrong with my method is that it gives me this error: undefined method `filter_my_rows'. Just like if it wasnt able to figure out that I am trying to call a method from within itself.Your method also gives me the same error: undefined method `filtered'
mickey
I fixed the error. The method should be self.filtered.
Simone Carletti
+3  A: 

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.

tadman
This worked for me partly. I dont fully understand lambda, I'll have to read on it, but the reject method will be pretty usefull to remove rows from the find array. Thanks
mickey