ruby - Rails how to create a class method self to keep things DRY? -
i repeat code many times. thing changes name in conditions.
find(:all, :conditions => {:name => 'hans'}, :group => 'dato').map(&:klik)
i trying make class method in model. can keep dry.
i have been trying , not working.
def self.grafdata(name) find(:all, :conditions => {:name => '(name)'}, :group => 'dato').map(&:klik) end
error: uninitialized constant actionview::compiledtemplates::lars
i want able write model.grafdata(hans), model.grafdata(lars)
i add function model:
class model def self.grafdata(name) where(name: name).group('dato').map(&:klik) end end
you can call either of following:
model.grafdata('lars') model.grafdata('hans')
Comments
Post a Comment