Rails: call method in model

It is impossible to understand this. In the rails model, I want to call a method in the same model to manipulate the data returned by the find method. This "filter" method will be called from many search methods in this model, so I want it to be separate. (and I can't filter from SQL, it's 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 

Result: undefined method `filter_my_rows'

Thanks for the help!

+6
ruby ruby-on-rails model
source share
4 answers

Part of the problem is that you are defining a class method called find_current and an instance method called filter_my_rows. As a rule, you define them both within the same field, and for collaboration.

Another thing is that you can do a lot of filtering that you need with a simple call to # Array # reject. 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 do this with modulation by adding functions if necessary, but it can get complicated if you are 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 is only necessary because of concerns that you cannot determine the relevance of a particular record before all records are downloaded from the database, this is usually a bad form, since you, you will probably reject a large amount of data that you encounter with the problem of obtaining and creating instances as models only to delete them immediately.

If possible, you should at least cache the received rows for the duration of the request so that you do not have to collect them all the time.

+4
source share

Class function and instance function is your problem.

You cannot call an instance function in your class function this way.

Use self.filter_my_rows to define your function (pay attention to self ), and everything will be correct.

+3
source share

use named_scope instead

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

then in your controller

 @date = Model.current 

you can also use lambda named_scopes functions

+2
source share

What is wrong with your decisions? 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 search methods within this model, so I want it to be separate.

... that you cannot 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 
0
source share

All Articles