質問編集履歴

3

モデルの中身をフルで書いた

2022/01/24 07:24

投稿

sia_yuki_f717
sia_yuki_f717

スコア1

test CHANGED
File without changes
test CHANGED
@@ -53,11 +53,58 @@
53
53
  ````
54
54
 
55
55
  ```ruby
 <music.rb>

56
+ class Music < ApplicationRecord
57
+ belongs_to :user
58
+ has_many :tracks, dependent: :destroy
59
+ has_many :comments, dependent: :destroy
60
+ has_many :favorites
56
- has_many :order
61
+ has_many :order
62
+ has_one_attached :image
63
+
64
+ with_options presence: true do
65
+ validates :title, length: { maximum: 22 }
66
+ validates :image
67
+ validates :artist_name, length: { maximum: 22 }
68
+ validates :price, format: { with: /\A[0-9]+\z/ },
69
+ numericality: { only_integer: true, greater_than_or_equal_to: 500, less_than_or_equal_to: 50_000 }
70
+ end
71
+
72
+ validates :cd_type_id, numericality: { other_than: 1 }
73
+
74
+ extend ActiveHash::Associations::ActiveRecordExtensions
75
+ belongs_to :cd_type
76
+
77
+ validate :image_content_type, if: :was_attached?
78
+
79
+ def image_content_type
80
+ extension = ['image/png', 'image/jpg', 'image/jpeg']
81
+ errors.add(:image, 'の拡張子が間違っています') unless image.content_type.in?(extension)
82
+ end
83
+
84
+ def was_attached?
85
+ image.attached?
86
+ end
87
+
88
+ def favorited_by?(user)
89
+ favorites.where(user_id: user.id).exists?
90
+ end
91
+ end
57
92
  ```
58
93
 
59
94
  ```ruby <order.rb>

95
+ class Order < ApplicationRecord
96
+ has_one_attached :image
97
+ attr_accessor :token
60
- belongs_to :music
98
+ belongs_to :music
99
+ belongs_to :user
100
+
101
+
102
+ with_options presence: true do
103
+ validates :token
104
+ validates :music_id
105
+ validates :user_id
106
+ end
107
+ end
61
108
  ```
62
109
  ordersテーブル

63
110
  https://gyazo.com/e44ce1f035a448db91a7bc9410af5818



2

music_idに変更

2022/01/23 12:03

投稿

sia_yuki_f717
sia_yuki_f717

スコア1

test CHANGED
File without changes
test CHANGED
@@ -41,7 +41,7 @@
41
41
  ```html <music/index>
42
42
  <div class="info_show4">
43
43
  <% if user_signed_in? && current_user.id != music.user_id %>
44
- <% if user_signed_in? && current_user.id == @order.user_id %>
44
+ <% if user_signed_in? && music.id == @order.music_id %>
45
45
  <%= @order.user_id %>
46
46
  <% else %>
47
47
  <%= link_to "¥#{music.price}", music_orders_path(music.id) %>

1

マークダウン記法変更

2022/01/22 15:03

投稿

sia_yuki_f717
sia_yuki_f717

スコア1

test CHANGED
File without changes
test CHANGED
@@ -58,8 +58,7 @@
58
58
 
59
59
  ```ruby <order.rb>

60
60
  
belongs_to :music
61
- ```
61
+ ```
62
-
63
62
  ordersテーブル

64
63
  https://gyazo.com/e44ce1f035a448db91a7bc9410af5818


65
64