has_many :through rails

折月煮酒 提交于 2019-12-12 02:47:09

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/33295256/has-many-through-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!