如何在 rails 中显示来自数据库的特定数据?
how display specific data from db in rails?
我有两个模型 user
和 speed
用户模型包含
name
,email
,password
,id_watch
这一切都来自注册 除了user_id
速度模型包含
id_watch
,x_speed
,y_speed
所有这些都已由手表设备添加
用户注册时需要然后输入id_watch
获取所有id_watch
数据
当 user table
的 id_watch
等于 speed table
的 id_watch
你可以这样创建smth。协会对此并不重要。
#view pages
<% @speed_data.each do |sd| %>
<%= "#{sd.x_speed} : #{sd.y_speed}" %>
<% end %>
#in controller where using current_user or user object
#sample
def index
@speed_data = current_user.speed_data
end
# models/user.rb
class User < ApplicationRecord
def speed_data
Speed.where(id_watch: self.id_watch)
end
end
# models/speed.rb
class Speed < ApplicationRecord
end
我有两个模型 user
和 speed
用户模型包含
name
,email
,password
,id_watch
这一切都来自注册 除了user_id
速度模型包含
id_watch
,x_speed
,y_speed
所有这些都已由手表设备添加
用户注册时需要然后输入id_watch
获取所有id_watch
数据
当 user table
的 id_watch
等于 speed table
id_watch
你可以这样创建smth。协会对此并不重要。
#view pages
<% @speed_data.each do |sd| %>
<%= "#{sd.x_speed} : #{sd.y_speed}" %>
<% end %>
#in controller where using current_user or user object
#sample
def index
@speed_data = current_user.speed_data
end
# models/user.rb
class User < ApplicationRecord
def speed_data
Speed.where(id_watch: self.id_watch)
end
end
# models/speed.rb
class Speed < ApplicationRecord
end