[英]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.