Skip to content

Instantly share code, notes, and snippets.

@soilforlifeforms
Created March 29, 2016 10:44
Show Gist options
  • Save soilforlifeforms/52e6f59d19ccf28bc2b3 to your computer and use it in GitHub Desktop.
Save soilforlifeforms/52e6f59d19ccf28bc2b3 to your computer and use it in GitHub Desktop.
class AfterTraining <ActiveRecord::Base
belongs_to :gardener
has_one :experience
has_one :network
has_one :veg_expenditure
has_one :health
end
class BeforeTraining <ActiveRecord::Base
belongs_to :gardener
has_one :experience
has_one :network
has_one :veg_expenditure
has_one :health
end
# having two seperate ids in experience table
create_table "experiences", force: true do |t|
t.boolean "previous_experience"
t.string "experience_qualitative"
t.boolean "currently_growing"
t.string "currently_growing_neg"
t.string "usage"
t.string "selling_amount"
t.boolean "other_organizations"
t.string "other_organizations_qualitative"
t.string "selling_qualitative"
t.integer "earning_power"
t.integer "garden_contribution"
t.integer "before_training_id"
t.string "selling_extra"
t.integer "after_training_id"
t.integer "before_training_id"
end
create_table "before_trainings", force: true do |t|
t.integer "gardener_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "after_trainings", force: true do |t|
t.integer "gardener_id"
t.datetime "created_at"
t.datetime "updated_at"
end
class Gardener < ActiveRecord::Base
belongs_to :group
has_one :before_training
has_one :after_training
#has_one :education, :through => :demographic
#has_one :employment, :through => :demographic
#has_one :grant, :through => :demographic
#has_many :documents
# has_attached_file :avatar, :styles => {:thumb => "100x100>"}
# validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
has_attached_file :avatar, dependent: :destroy,
:storage => :dropbox,
:dropbox_credentials => "#{Rails.root}/config/dropbox.yml",
:styles => {:medium => "300x300>", :thumb => "100x100>"},
:default_url => "/images/missing_pic.jpeg",
:dropbox_options => {
:path => proc { |style| "#{style}/#{id}_#{avatar.original_filename}" },
:unique_filename => true
}
validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
validates :first_name, length: {minimum: 2, too_short: "First name needs at least 2 characters"}
validates :last_name, length: {minimum: 2, too_short: "Last name needs at least 2 characters"}
validates :contact_number, length: {minimum: 9, too_short: "Contact numbers need to have 10 characters", maximum: 11, too_long: "Contact numbers need to have 10 characters"}
validates :address, length: {minimum: 4, too_short: "Address needs at least 4 characters"}
validates :id_number, :length => {:is => 13,
:too_short => "must have at least %{count} words",
:too_long => "must have at most %{count} words"}
validates_uniqueness_of :id_number, unless: '1111111111111'
validates :race, :presence => true
#has_attached_file :resume, dependent: :destroy,
# :storage => :dropbox,
# :dropbox_credentials => "#{Rails.root}/config/dropbox.yml"
# Counts gardeners with support visit count equals to the number or in the range.
# Example:
# Gardener.count_by_support_visit_count(4)
# Gardener.count_by_support_visit_count(1..3)
# Gardener.count_by_support_visit_count(0)
def self.count_by_support_visit_count(range_or_number)
range = if range_or_number.is_a?(Range)
range_or_number
else
(range_or_number)..(range_or_number)
end
query = %{
SELECT COUNT(*)
FROM
(
SELECT g.id
FROM gardeners g
LEFT JOIN support_visits sv ON sv.gardener_id = g.id
GROUP BY g.id
HAVING COUNT(sv.gardener_id) BETWEEN #{range.first} AND #{range.last}
) AS gardeners;
}
count_by_sql(query)
end
def self.races
%w(black coloured indian white other)
end
def self.genders
%w(male female)
end
def full_name
"#{first_name} #{last_name}"
end
end
#mount_uploader :image, ImageUploader
#def active?
# status == 'active'
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment