Skip to content

Instantly share code, notes, and snippets.

@i8degrees
Created January 24, 2012 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i8degrees/1667733 to your computer and use it in GitHub Desktop.
Save i8degrees/1667733 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Note filing system implemented via many-to-many relation sharing tags
CREATE TABLE notes (note_id INT UNSIGNED NOT NULL AUTO_INCREMENT, # Primary key => note.note_id
note VARCHAR(255) NOT NULL,
PRIMARY KEY (note_id))
# ENGINE=InnoDB;
# (3NF) Notes <-> Tags mapping / routing implementation; no worries of data duplication / debris being left behind
CREATE TABLE mappings (map_id INT UNSIGNED NOT NULL AUTO_INCREMENT, # Primary key => mapping.map_id
note_id INT UNSIGNED NOT NULL, # Foreign key => notes.note_id
tag_id INT UNSIGNED NOT NULL, # Foreign key => tags.tag_id
PRIMARY KEY (map_id),
INDEX (note_id, tag_id)) # Indexes => (notes.id, tags.id)
# ENGINE=InnoDB;
# (Practically :-P) Simplest tag filing implementation both possible & desired
CREATE TABLE tags (tag_id INT UNSIGNED NOT NULL AUTO_INCREMENT, # Primary key => tag.tag_id
tag VARCHAR(64) NOT NULL,
PRIMARY KEY (tag_id), INDEX (tag)) # Indexes => (tags.tag)
# ENGINE=InnoDB;
---
---
get '/show/:note' do
note = Note.first(:name => params[:note])
if note !=nil
tags = note.tags.all
if tags !=nil
tags.flat_map {|tag| " " << tag.name << ", " }
else
"nil"
end
else
"nil"
end
end
---
if @pastie.save
redirect "/#{@pastie.id}"
else
redirect '/'
end
---
\# This class is responsible for the Books REST interface.
\#
class BooksController < ApplicationController
\# Get a book by the id
before_filter :get_book, only: [:edit, :update, :show, :destroy]
\# Renders the form to create a new Book.
\#
def new
@book = Book.new
end
\# Creates a new Book from the params and redirects to edit view.
\#
def create
@book = Book.create(params[:book])
redirect_to book_path(@book)
end
\# Renders the form for a given book.
\#
def edit
end
\# Updates a Book from the params and redirects to edit view.
\#
def update
@book.update(params[:book])
redirect_to edit_book_path(@book)
end
\# Renders all books
\#
def index
@books = Book.all
end
\# Renders a Book
\#
def show
end
\# Destroys the Book object from database
\#
def destroy
@book.destroy
redirect_to action: :index
end
private
def get_book
@book = Book.get(params[:id])
end
end
---
[*] Fat Music Volume I: Fat Music for Fat People (1994)
[*] Fat Music Volume III: Physical Fatness (1997)
[*] Fat Music Volume IV: Life in the Fat Lane (1999)
[*] Fat Music Volume V: Live Fat, Die Young (2001)
[+] Fat Music Volume II: Survival of the Fattest (1996)
[+] Fat Music Volume VI: Uncontrollable Fatulence (2002)
[+] Fat Music Volume VII: Harder, Fatter + Louder! (2010)
[+] Wrecktrospective (2009)
---
has n, :mappings, 'Mapping'
#:parent_key => { :note_id },
#:child_key => { :tag_id }
has n, :
#belongs_to :user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment