的has_many:通过轨道 [英] has_many :through rails
问题描述
我想USER1接受来自其他用户的请求加入user1s岗位作为贡献者,并列出,如果用户1接纳他们。
似乎无法得到它的工作 - 这是我的设置:
的has_many:通过
我在想:
岗位模型
类岗位<的ActiveRecord :: Base的
#Join台协会
的has_many:group_requests,将class_name:「本集团」,
foreign_key:requester_id
相关:摧毁
的has_many:group_users,将class_name:「本集团」,
foreign_key:accepted_id
相关:摧毁
的has_many:索取者,通过:group_requests
的has_many:接受,通过:group_users
高清请求(other_post)
group_requests.create(accepted_id:other_post.id)
结束
#Unfollows用户。
高清unrequest(other_post)
group_requests.find_by(accepted_id:other_post.id).destroy
结束
#如果当前用户是跟随其他用户,则返回true。
DEF接受?(other_post)
requesting.include?(other_post)
结束
用户模型
类用户的LT;的ActiveRecord :: Base的
#Join台协会
belongs_to的:组
组模型
类组<的ActiveRecord :: Base的
belongs_to的:索取者,将class_name:邮报
belongs_to的:接受,将class_name:邮报
验证:requester_id,presence:真
验证:accepted_id,presence:真
结束
CreatePosts迁移
类CreatePosts< ActiveRecord的::迁移
高清变化
CREATE_TABLE:帖子做| T |
t.string:标题
t.text:内容
t.timestamps空:假的
结束
结束
结束
DeviseCreateUsers迁移
类DeviseCreateUsers< ActiveRecord的::迁移
高清变化
CREATE_TABLE(:用户)办| T |
##数据库可验证
t.integer:的post_id
t.string:电子邮件,空:假的,默认:
AddUserIdToPosts
类AddUserIdToPosts< ActiveRecord的::迁移
高清变化
add_column:帖子,:USER_ID,整数
add_index:帖子,:USER_ID
结束
结束
CreateGroup迁移
类CreateGroups< ActiveRecord的::迁移
高清变化
CREATE_TABLE:组办| T |
t.integer:requester_id
t.integer:accepted_id
t.timestamps空:假的
结束
add_index:组:requester_id
add_index:组:accepted_id
add_index:组,[:requester_id,:accepted_id],独特的:真正的
结束
结束
邮政控制器
DEF接受
@title =接受
@post = Post.find(PARAMS [:ID])
@users = @ user.accepted.paginate(页:PARAMS [:页面])
渲染show_accepted
结束
DEF请求
@title =请求者
@post = Post.find(PARAMS [:ID])
@users = @ user.requester.paginate(页:PARAMS [:页面])
渲染show_requester
结束
私人
高清post_params
params.require(:岗位).permit(:标题,:内容:图片)
结束
展后
<%@post || = CURRENT_USER(@post)%>
< DIV CLASS =统计>
&所述; A HREF =&其中;%= accepted_post_path(@post)%>中>
<强ID =下面的类=STAT>
&其中;%= @ post.accepted.count%GT;
< / STRONG>
以下
&所述; / a取代;
&所述; A HREF =&其中;%= requester_post_path(@post)%>中>
<强ID =追随者级=STAT>
&其中;%= @ post.requester.count%GT;
< / STRONG>
追随者
&所述; / a取代;
< / DIV>
有什么建议?
您可以试试这个---
因为我觉得p是您的文章,并希望得到所有user_ids。
p.group_members.map {|克| gm.user_id}在阵列中所有的id #list
###或者试试这个
在阵列中的所有用户p.users #list
#如果你想收集IDS则
p.users.collect(安培;:ID)
我觉得应该对你有所帮助。
I want to get user1 to accept requests from other users to join user1s post as contributors and be listed out if user1 accepts them
Can't seem to get it to work - Here's my setup:
has_many :through
I'm thinking:
post model
class Post < ActiveRecord::Base
#Join table associations
has_many :group_requests, class_name: "Group",
foreign_key: "requester_id",
dependent: :destroy
has_many :group_users, class_name: "Group",
foreign_key: "accepted_id",
dependent: :destroy
has_many :requester, through: :group_requests
has_many :accepted, through: :group_users
def request(other_post)
group_requests.create(accepted_id: other_post.id)
end
# Unfollows a user.
def unrequest(other_post)
group_requests.find_by(accepted_id: other_post.id).destroy
end
# Returns true if the current user is following the other user.
def accepted?(other_post)
requesting.include?(other_post)
end
user model
class User < ActiveRecord::Base
#Join table associations
belongs_to :group
group model
class Group < ActiveRecord::Base
belongs_to :requester, class_name: "Post"
belongs_to :accepted, class_name: "Post"
validates :requester_id, presence: true
validates :accepted_id, presence: true
end
CreatePosts Migration
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.text :content
t.timestamps null: false
end
end
end
DeviseCreateUsers Migration
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Database authenticatable
t.integer :post_id
t.string :email, null: false, default: ""
AddUserIdToPosts
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_column :posts, :user_id, :integer
add_index :posts, :user_id
end
end
CreateGroup Migration
class CreateGroups < ActiveRecord::Migration
def change
create_table :groups do |t|
t.integer :requester_id
t.integer :accepted_id
t.timestamps null: false
end
add_index :groups, :requester_id
add_index :groups, :accepted_id
add_index :groups, [:requester_id, :accepted_id], unique: true
end
end
Post Controller
def accepted
@title = "Accepted"
@post = Post.find(params[:id])
@users = @user.accepted.paginate(page: params[:page])
render 'show_accepted'
end
def requester
@title = "Requesters"
@post = Post.find(params[:id])
@users = @user.requester.paginate(page: params[:page])
render 'show_requester'
end
private
def post_params
params.require(:post).permit(:title, :content, :image)
end
Post Show
<% @post ||= current_user(@post) %>
<div class="stats">
<a href="<%= accepted_post_path(@post) %>">
<strong id="following" class="stat">
<%= @post.accepted.count %>
</strong>
following
</a>
<a href="<%= requester_post_path(@post) %>">
<strong id="followers" class="stat">
<%= @post.requester.count %>
</strong>
followers
</a>
</div>
Any suggestions?
You could try this ---
as i think p is your post and you want to get all user_ids.
p.group_members.map{|gm|gm.user_id} #list of all ids in array
### OR try this
p.users #list of all users in array
#if you want to collect ids then
p.users.collect(&:id)
I think it should be helpful for you
这篇关于的has_many:通过轨道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!