On Jul 1, 1:26 pm, "Älphä Blüë" <[email protected]>
wrote:
> Hi all,
>
> I just have a question about shared model methods. I have a method that
> will be used for quite a few models in my application (once I've cleaned
> it up a bit). Where can I place shared model methods? I know that
> controllers can use helpers or even the application_controller. But,
> what do models use for a shared mechanism? Is it config/initialize?
Yes, just use Ruby modules.
module SharedMethods
def list
# ...
end
end
And in your model,
class MyModel < ActiveRecord::Base
extend SharedMethods
end
Does this help?
Jeff
Rails for Everyone: Oct 24, 2009 in Chicago:
http://www.purpleworkshops.com/workshops/rails-for-everyone
> Can someone explain a quick process for it?
>
> Here's an example of a shared model method that I will be using:
>
> def self.list(search, page, orderby, sortby, numteams, datevar)
> datevar = todaysdate if datevar.empty? || datevar.nil? ||
> DATE_VALIDATE_REG.match(datevar).nil?
> convert_time = datevar.to_time.beginning_of_week
> week_start_date = convert_time.to_date.strftime('%Y-%m-%d')
> named_scope :compiled_on_week, lambda { { :conditions =>
> ['compiled_on >= ? and compiled_on <= ?', week_start_date, datevar] } }
> orderby = "rank" if orderby.nil? || orderby.empty?
> orderall = (sortby != 'asc' && sortby != nil) ? "DESC" : "ASC"
> compiled_on_week.paginate :joins => :team, :conditions => ['name
> like ?', "%#{search}%"], :order => orderby + " #{orderall}", :per_page
> => numteams, :page => page
> end
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---