File size: 840 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Post < ActiveRecord::Base
  validates :body, :author_id,:wall_id ,presence: true

  has_attached_file :photo, default_url: "missing-post.png"
  validates_attachment_content_type :photo, content_type: /\Aimage\/.*\Z/

  belongs_to :wall,
  foreign_key: :wall_id,
  primary_key: :id,
  class_name: :User

  belongs_to :author,
  class_name: :User,
  primary_key: :id,
  foreign_key: :author_id

  has_many :comments,
  class_name: :Comment,
  primary_key: :id,
  foreign_key: :post_id

 def self.find_posts_by_wall_id(id)
   Post.all.where("wall_id = id").order("created_at DESC")
 end

 def self.find_posts_of_friends(id)
   friend_ids = User.find(id).accepted_friends_ids

   friend_ids_string = "(#{friend_ids.join(', ')})"

   Post.where("author_id IN 'friend_ids_string'")+
   Post.where("wall_id IN 'friend_ids_string'")
 end
end