[英]Getting all users from db that participated for an activity
我目前面臨以下問題:用戶能夠創建活動。 其他用戶有可能參加活動。 現在,我希望已在活動視圖中列出已經參加活動的用戶。 因此,我想做的是使用activity_id向顯示在activity_controller的show方法中參加某個活動的所有用戶。
模型之間的關系如下:
class Activity < ActiveRecord::Base
has_many :users, through: :participations
belongs_to :user
has_many :participations
has_many :reviews
end
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :activity
has_many :activities
end
class User < ActiveRecord::Base
has_many :activities
has_many :participations
has_many :reviews
end
我的活動控制器ist執行以下操作:
class ActivitiesController < ApplicationController
before_action :set_activity, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:show]
def index
@activities = Activity.all
end
def show
@participated = Participation.where("activity_id = ? AND user_id = ?", @activity.id, current_user.id).present? if current_user
@reviews = @activity.reviews
@hasReview = @reviews.find_by(user_id: current_user.id) if current_user
@participant = Participation.where (:activity_id == 'activity_id')
end
def new
@activity = current_user.activities.build
end
def create
@activity = current_user.activities.build(activity_params)
if @activity.save
redirect_to edit_activity_path(@activity), notice: "We want to give you a warm welcome!"
else
render :new, notice: "Something seems to be missing!"
end
end
def edit
if current_user.id == @activity.user.id
redirect_to root_path
end
end
def update
if @activity.update(activity_params)
redirect_to edit_activity_path(@activity), notice: "Updated!"
else
render :edit
end
end
def destroy
@activity = Activity.find(params[:id])
if @activity.destroy
redirect_to activities_path, notice: "Deleted!"
end
end
private
def set_activity
@activity = Activity.find(params[:id])
end
def activity_params
params.require(:activity).permit(:activity_name,
:activity_starttime,
:activity_duration,
:activity_intensity,
:activity_distance,
:activity_topography,
:activity_address,
:activity_track,
:activity_summary)
end
end
同時參加者負責處理:
class ParticipationsController < ApplicationController
before_action :authenticate_user!
def preload
activity= Activity.find(params[:activity_id])
participations = activity.participations
render json: participations
end
def create
if Participation.exists?(user_id: current_user.id)
redirect_to activities_path(@activity), notice: "Du nimmst bereits teil!"
else
@participation = current_user.participations.create(participation_params)
redirect_to @participation.activity, notice: "Klasse, du nimmst nun teil!"
end
end
def participant
@participant = user.participations
end
def your_matches
@matches = current_user.participations
end
def your_participations
@activities = current_user.activities
end
private
def participation_params
params.permit(:activity_id, :status)
end
end
在我看來,我通過使用以下命令調用了控制器:
<div class="row">
<div class="col-md-12">
<h3>Wer kommt bis jetzt mit? </h3>
<table>
<tr>
<th></th>
</tr>
<% @participant.each do |participant| %>
<tr>
<td><%= participant.user.fullname %></td>
</tr>
<% end %>
</table>
</div>
</div>
這列出了參與任何活動的所有用戶的列表。 但是,我現在唯一想要的是顯示所有參加所請求活動的用戶。 我會說我的關系應該正常工作,因此我想我只需要一個小提示即可解決問題。 在我的應用程序的另一點,我已經有了一個列表,可以在其中顯示參與的用戶,但是由於控制器的不同,我無法從活動控制器中調用它。 總而言之,這會導致錯誤,因為我無法從activitities_view調用Partitions_controller。
<div class="panel-body">
<% @activities.each do |activity| %>
<% activity.participations.each do |participation| %>
<div class="row">
<div class="col-md-3">
<%= link_to user_path(participation.user) do %>
<%= participation.user.fullname %>
<% end %>
</div>
衷心感謝您的任何幫助,並在此先感謝!
編輯1:有關數據庫包括的更多信息:
class CreateParticipations < ActiveRecord::Migration
def change
create_table :participations do |t|
t.references :user, index: true, foreign_key: true
t.references :activity, index: true, foreign_key: true
t.string :status
t.timestamps null: false
end
end
end
class CreateActivities < ActiveRecord::Migration
def change
create_table :activities do |t|
t.string :activity_name
t.datetime :activity_starttime
t.time :activity_duration
t.string :activity_intensity
t.integer :activity_distance
t.string :activity_topography
t.string :activity_address
t.string :activity_track
t.string :activity_summary
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
我仍然不明白何時在參考Activity
模型的情況下在Participation
模型中添加has_many
和belongs_to
遷移。
如果您有:
has_many
參與 has_many
參股 belongs_to
用戶和belongs_to
活動 然后,您可以將User
with Participation
加入到查詢中,並嘗試使所有參與特定活動的用戶都參與其中,例如:
User.joins(:participations).where('participations.activity_id = 1')
編輯:我已經測試了您的代碼,沒有在模型,遷移等方面保持不變。我插入了一些示例記錄,例如:
User.new(fullname: 'seb', ...).save
User.new(fullname: 'sab', ...).save
User.new(fullname: 'frank', ...).save
Activity.new(name: 'yoga', user_id: 1).save
Activity.new(name: 'pilates', user_id: 1).save
Activity.new(name: 'tennis', user_id: 2).save
Activity.new(name: 'etc etc etc', user_id: 3).save
Participation.new(user_id: 1, activity_id: 1).save
Participation.new(user_id: 2, activity_id: 1).save
Participation.new(user_id: 3, activity_id: 1).save
然后運行查詢,讓3位用戶參與了ID為3的活動:
User.joins(:participations).where('participations.activity_id = 1').pluck(:fullname)
我得到:
SELECT "users"."fullname" FROM "users" INNER JOIN "participations" ON "participations"."user_id" = "users"."id" WHERE (participations.activity_id = 1)
# => ["seb", "sab", "frank"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.