Skip to content

Instantly share code, notes, and snippets.

@Jimgerneer
Created December 24, 2012 19:56
Show Gist options
  • Save Jimgerneer/4370500 to your computer and use it in GitHub Desktop.
Save Jimgerneer/4370500 to your computer and use it in GitHub Desktop.
Attempts at scope
1 class Perpetrator < ActiveRecord::Base¬
2 attr_accessible :name¬
3 ¬
4 scope :active_reports, joins(:reports).where(['report.active = ?', true])¬
5 ¬
6 has_many :reports¬
7 ¬
8 def to_s¬
9 name¬
10 end¬
11 end¬
1 class Report < ActiveRecord::Base¬
2 attr_accessible :user_id, :evidence, :location, :time, :perpetrator_id, :new_perpetrator, :bounty, :active¬
3 attr_accessor :new_perpetrator¬
4 ¬
5 before_validation :create_new_perpetrator¬
6 ¬
7 belongs_to :perpetrator¬
8 belongs_to :user¬
9 ¬
10 scope :active, where(active: true)¬
11 scope :recent, order('created_at DESC')¬
12 scope :for_perp, lambda{|perp_id| where(perpetrator_id: perp_id)}¬
13 scope :for_author, lambda{|user_id| where(user_id: user_id)}¬
14 ¬
15 def close¬
16 self.active = false¬
17 self.save¬
18 end¬
19 ¬
20 private¬
21 ¬
22 def create_new_perpetrator¬
23 unless perpetrator_id.present?¬
24 return false unless new_perpetrator.present?¬
25 self.perpetrator = Perpetrator.find_or_create_by_name(new_perpetrator)¬
26 end¬
27 end¬
28 end¬
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment