Skip to content

Instantly share code, notes, and snippets.

View Sailias's full-sized avatar

Jonathan Salis Sailias

  • Bus Patrol
  • Toronto Canada
View GitHub Profile
1. def find_commentable
2. params.each do |name, value|
3. if name =~ /(.+)_id$/
4. return $1.classify.constantize.find(value)
5. end
6. end
7. nil
8. end
>> @article = Article.new
@article = Article.new
=> #<Article id: nil, created_at: nil, updated_at: nil>
>> @article.comments.build(:content=>"Hello")
@article.comments.build(:content=>"Hello")
=> #<Comment id: nil, created_at: nil, updated_at: nil, content: "Hello", commentable_id: nil, commentable_type: "Article">
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic=>true
end
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
###############################################
@comment.update_attributes(:commentable_id=>nil, :commentable_type=>nil)
=> true
>> @comment.reload
@comment.reload
=> #<Comment id: 2, created_at: "2010-04-29 14:54:07", updated_at: "2010-04-29 15:05:32", content: "Hello", commentable_id: nil, commentable_type: nil>
>> @comment.commentable
# AJAX request: This adds line items to cart.
def add_line_item_to_cart
#Get current_cart object
current_cart
quantity = params[:quantity].to_i
id = params[:id].to_i
line_item = LineItem.find_or_create_by_cartridge_id_and_cart_id(id, current_cart.id)
line_item.quantity = line_item.quantity.to_i + quantity
def show
authorize! :assignment, :show
@assignment = Assignment.find(params[:id])
@related_logs = @assignment.assignment_logs.page(params[:page]).per(4).order('assignment_logs.date DESC').includes(:user)
# do a find to find and order candidates by there current employment
@candidates = AssignmentList.where('assignment_lists.assignment_id = ?', @assignment.id)
scope :current, where(:current=>true}
validate :only_one_current
protected
def only_one_current
errors.add_to_base("Only one event can be current") if Event.current.reject{|c| c.eql?(self)}.size>0
end
<%= select_tag :other_select, options_for_select(@collection) %>
SELECT my_ships.id as ship_id, (
SELECT planets.id
FROM planets
WHERE id NOT IN(SELECT id FROM planets WHERE conqueror_id=2599)
ORDER BY SQRT(((planets.location_x::bigint+-my_ships.location_x::bigint)^2) + ((my_ships.location_y::bigint-planets.location_y::bigint)^2))
ASC OFFSET 0 LIMIT 1) as planet_id
FROM my_ships;
schemaverse=> NOTIFY jdcyekurqi, 'some text';
NOTIFY
schemaverse=> SELECT 1;
?column?
----------
1
(1 row)