Skip to content

Instantly share code, notes, and snippets.

@RyanFriedman
Created January 8, 2012 22:23
Show Gist options
  • Save RyanFriedman/1579930 to your computer and use it in GitHub Desktop.
Save RyanFriedman/1579930 to your computer and use it in GitHub Desktop.
class Post < ActiveRecord::Base
attr_accessor :position, :follower_id
has_many :relationships, :dependent => :destroy,
:foreign_key => "follower_id"
has_many :reverse_relationships, :dependent => :destroy,
:foreign_key => "followed_id",
:class_name => "Relationship"
has_many :followers, :through => :reverse_relationships,
:source => :follower
accepts_nested_attributes_for :relationships
end
class Relationship < ActiveRecord::Base
attr_accessible :followed_id, :latitude
belongs_to :follower, :class_name => "User"
belongs_to :followed, :class_name => "Post"
validates :follower_id, :presence => true
validates :followed_id, :presence => true
end
class RelationshipsController < ApplicationController
def sort
params[:post].each_with_index do |id, index|
Relationship.update_all({position: index+1}, {id: id})
end
render nothing: true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment