Rails: call method within model

16,555

Solution 1

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.

Solution 2

function of class and function of instance is your problem.

You can't call a instance function in your class function that way.

Use self.filter_my_rows to define your function (note the self) and everything will go right.

Solution 3

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

Share:
16,555
mickey
Author by

mickey

Updated on June 09, 2022

Comments

  • mickey
    mickey almost 2 years

    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!