Skip to content

Instantly share code, notes, and snippets.

@basti
Created August 7, 2011 07:52
Show Gist options
  • Save basti/1130173 to your computer and use it in GitHub Desktop.
Save basti/1130173 to your computer and use it in GitHub Desktop.
class News < ActiveRecord::Base
has_many :quotes, :autosave=>true, :dependent=>:destroy, :order => "position"
MAX_QUOTES = 3
before_save :remove_excess_quotes
protected
def remove_excess_quotes
if self.quotes.count > MAX_QUOTES
self.quotes.destroy_all ["position > ?", MAX_QUOTES] # nisam siguran da li ["",...] sintaksa radi za destroy_all - trebalo bi, ako ne radi samo napravi obican string
end
end
end
class Quote < ActiveRecord::Base
belongs_to :news
# https://github.com/swanandp/acts_as_list
# pogledaj i sam source da vidis koje sve metode ima:
# https://github.com/swanandp/acts_as_list/blob/master/lib/acts_as_list/active_record/acts/list.rb
acts_as_list :scope => :news
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment