Files
growstuff/app/models/crop.rb
2013-05-02 13:27:12 +10:00

37 lines
759 B
Ruby

class Crop < ActiveRecord::Base
extend FriendlyId
friendly_id :system_name, use: :slugged
attr_accessible :en_wikipedia_url, :system_name
has_many :scientific_names
has_many :plantings
default_scope order("lower(system_name) asc")
validates :en_wikipedia_url,
:format => {
:with => /^https?:\/\/en\.wikipedia\.org\/wiki/,
:message => 'is not a valid English Wikipedia URL'
}
def Crop.random
@crop = Crop.offset(rand(Crop.count)).first
return @crop
end
def to_s
return system_name
end
def default_scientific_name
if scientific_names.count > 0
return scientific_names.first.scientific_name
else
return nil
end
end
def plantings_count
return plantings.count
end
end