gpt4 book ai didi

ruby-on-rails - 从参与事件的数据库中获取所有用户

转载 作者:太空宇宙 更新时间:2023-11-03 16:42:11 25 4
gpt4 key购买 nike

我目前面临以下问题:用户能够创建事件。其他用户有可能参与该事件。我现在希望已经参与事件的用户将列在事件 View 中。所以我尝试的是用 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

我的事件 Controller 正在执行以下操作:

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

同时参与 Controller 处理这个:

    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

在我看来,我调用了 Controller :

<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>

这为我提供了参与任何事件的所有用户的列表。但我现在唯一想要的是显示所有参与请求事件的用户。我会说我的关系应该正常工作,因此我想我只需要一个小提示就可以解决问题。在我的应用程序的另一点,我已经得到了一个列表,我可以在其中显示参与的用户,但由于 Controller 不同,我无法从事件 Controller 中调用它。总而言之,这会导致错误,因为我无法从 activities_view 调用 participations_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

最佳答案

当你在你的Participation模型中添加一个has_manybelongs_to relantionship并引用Activity时,我还是不明白模型。

如果你有:

  • 用户 有_many 次参与
  • 事件 has_many参与
  • 参与belongs_to用户和belongs_to事件

然后您可以在查询中加入 UserParticipation 并尝试让所有参与特定事件的用户像:

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"]

关于ruby-on-rails - 从参与事件的数据库中获取所有用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44503450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com