我一直在這一整個下午工作,我非常接近,但我的大腦只是跳過簡單的東西,但我看不到它。Rails關聯 - 值是正確的,但關聯是零
在這個網站上,用戶可以創建和加入俱樂部。當用戶加入俱樂部時,他們加入俱樂部成員。俱樂部會員可以向俱樂部發帖。出於某種原因,我無法讓我的俱樂部成員發佈協會正常工作。值正在填寫正確的ID,但是當我檢查控制檯中的關聯時,我得到零。我的目標是能夠獲得發佈帖子的俱樂部成員的用戶名。
用戶模型
has_many :club_memberships, :class_name => 'ClubMembership', :foreign_key => :member_id, :primary_key => :id
has_many :clubs, :through => :club_memberships
俱樂部模式
has_many :club_memberships
has_many :members, :through => :club_memberships
has_many :posts
俱樂部會員制模式
belongs_to :club
belongs_to :member, :class_name => "User", :foreign_key => :member_id, :primary_key => :id
has_many :posts
郵政型號
belongs_to :club
belongs_to :club_membership
個
相關路由
resources :clubs do
resources :posts
end
resources :club_memberships, :only => [:index, :create, :destroy]
帖子控制器
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :set_club
before_action :set_membership_id
def new
@post = Post.new
end
def show
@posts = Post.find(params[:id])
end
def create
@post = Post.new(post_params)
@post.member_id = @club_membership.id
@post.club_id = @club.id
if @post.save
redirect_to @club
else
render 'new'
end
end
private
def set_post
@post = Post.find(params[:id])
end
def set_club
@club = Club.find(params[:club_id])
end
def set_membership_id
@club_membership = @club.club_memberships.find_by(member_id: current_user)
end
def post_params
params.require(:post).permit(:title, :postcontent)
end
end
該柱通過以下從球杆頁面的new_club_post_path(@club)的link_to創建。帖子創建正確,值正確。
最近的文章中的控制檯結果
> @post = Post.find(7)
Post Load (0.2ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 7 LIMIT 1
=> #<Post id: 7, member_id: 29, club_id: 22, title: "fdsafsadfas", postcontent: "fsdafsadfsadfsadfasdf", created_at: "2015-10-16 22:39:28", updated_at: "2015-10-16 22:39:28">
呼叫@ post.club返回true
> @post.club
Club Load (0.2ms) SELECT `clubs`.* FROM `clubs` WHERE `clubs`.`id` = 22 LIMIT 1
=> #<Club id: 22, club_name: "new test club", club_type: "Movies", created_at: "2015-10-16 22:34:51", updated_at: "2015-10-16 22:34:51">
但是當我嘗試@ post.club_membership,結果是零,我想不通爲什麼那是。
> @post.club_membership
=> nil
如果我嘗試設置一個特定值@club,我可以叫@ club.club_memberships所以我認爲協會工作正常。我只是遇到了一個問題,從郵寄到club_membership。我希望調用類似@post.club_membership.username
的東西,並讓該值返回相應的帖子的用戶名。如果我現在打電話,我得到
NoMethodError: undefined method `username' for nil:NilClass
謝謝!
edit-- 這是創建郵件表的遷移。
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.integer :member_id
t.integer :club_id
t.string :title
t.text :postcontent
t.timestamps null: false
end
end
end
修訂答 由於本網站上的用戶真棒,我能想出解決辦法。
我的帖子型號應該有這樣的聯想:
belongs_to :club_membership, :class_name => 'ClubMembership', :foreign_key => :member_id, :primary_key => :id
,它可以讓我叫@post.club_membership.member.username
拿到海報顯示的用戶名。
你有一個post.member_id方法,它看起來應該是一個ClubMembership,但是你沒有明確地在post模型中聲明外鍵。它看起來像member_id是指俱樂部會員模型上的用戶。 – Swards
我剛更新了帖子遷移。老實說,我對外鍵非常困惑,所以我試圖將它們排除在外,除非我找到了別人以類似方式使用它的東西,我可以弄明白。這對我來說可能是一種可怕的學習方式,但我會到達那裏。 – mochimantis
在這種情況下 - 我建議你將外鍵匹配到表中。外鍵只是它們關聯的類的ID。 Belongs_to是唯一有外鍵的。如果該列與_id後綴相同,則不需要顯式外鍵 – Swards