samedi 18 mars 2017

Twitter-Like "Reply and Retweet" Rails 3

I'm building a sample rails application that mimics some of my favorite apps. I want to implement a photo reply to a photo and a repost of a photo -- Similar to the way you reply and retweet via Twitter. Should I make a separate relationship model that captures the photo replies and reposts or should I handle everything in the photo model.

class Photo < ActiveRecord::Base

   attr_accessible :caption, :source, :source_remote_url, :source_file_name, 

   has_attached_file :source
   validates_attachment_content_type :source 

   belongs_to :user

   has_many :replies
   has_many :reposts  

  def source_remote_url=(url_value)
    self.source = URI.parse(url_value) unless url_value.blank?
   super
  end

 def replies
   join_table 
 end

 def replies?(other_user)
   join_table.find_by_replied_id(other_user.id)
 end

 def reply!(other_user)
   join_table.create!(replied_id: other_user.id)
 end

Aucun commentaire:

Enregistrer un commentaire