teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

追記依頼の、アソシエーション部分を追記

2019/12/02 00:27

投稿

garta
garta

スコア15

title CHANGED
File without changes
body CHANGED
@@ -46,4 +46,61 @@
46
46
  <% end %>
47
47
  </span>
48
48
  <% end %>
49
+ ```
50
+ ###like.rb
51
+ ```ここに言語を入力
52
+ class Like < ApplicationRecord
53
+ belongs_to :user
54
+ belongs_to :micropost
55
+ validates :user_id, presence: true
56
+ validates :like_id, presence: true
57
+ end
58
+ ```
59
+ ###user.rb
60
+ ```ここに言語を入力
61
+ class User < ApplicationRecord
62
+ has_many :microposts,dependent: :destroy
63
+ has_many :active_relationships, class_name: "Relationship",
64
+ foreign_key: "follower_id",
65
+ dependent: :destroy
66
+ has_many :passive_relationships, class_name: "Relationship",
67
+ foreign_key: "followed_id",
68
+ dependent: :destroy
69
+ has_many :following, through: :active_relationships, source: :followed
70
+ has_many :followers, through: :passive_relationships
71
+ has_many :likes, dependent: :destroy
72
+
73
+ validates :name,presence: :true, length:{ maximum: 15 }
74
+ validates :email,presence: :true
75
+ validates :password, presence:true,length:{ minimum: 6 }
76
+ validates :password_confirmation,presence: :true
77
+
78
+ devise :database_authenticatable, :registerable,
79
+ :recoverable, :rememberable, :validatable
80
+
81
+ mount_uploader :avatar, AvatarUploader
82
+ ```
83
+ ###micropost.rb
84
+ ```ここに言語を入力
85
+ belongs_to :user
86
+ has_many :likes, dependent: :destroy
87
+ has_many :like_users, through: :likes, source: :user
88
+ default_scope -> { order(created_at: :desc) }
89
+ mount_uploader :picture, PictureUploader
90
+ validates :user_id,presence: true
91
+ validates :title, presence: true
92
+ validates :content,length: { maximum: 140 }
93
+ validates :picture, presence: true
94
+
95
+ ```
96
+ ###relationship.rb
97
+ ```ここに言語を入力
98
+ class Relationship < ApplicationRecord
99
+ belongs_to :follower, class_name: "User"
100
+ belongs_to :followed, class_name: "User"
101
+
102
+ validates :follower_id,presence: true
103
+ validates :followed_id,presence: true
104
+ end
105
+
49
106
  ```