Rails 关联 - 值正确,但关联为零
Rails Associations - Values are correct, but association is nil
我一直在研究这个下午,我很接近,但我的大脑只是跳过一些简单的东西,但我看不到它。
在此站点上,用户可以创建和加入俱乐部。当用户加入俱乐部时,他们将作为俱乐部成员加入。俱乐部成员可以向俱乐部发出 posts。出于某种原因,我无法让 post 协会的俱乐部成员正常工作。该值正在用正确的 ID 填充,但是当我在控制台中检查关联时,我得到的是 nil。我的目标是能够获得创建 post.
的俱乐部成员的用户名
用户模型
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
Post 型号
belongs_to :club
belongs_to :club_membership
相关路线
resources :clubs do
resources :posts
end
resources :club_memberships, :only => [:index, :create, :destroy]
Post控制器
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
post 是通过关注俱乐部页面上的 new_club_post_path(@club) link_to 创建的。 post 已正确创建且值正确。
最近 post
的控制台结果
> @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 return是真的
> @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 所以我认为关联工作正常。我只是在从 post 到 club_membership 时遇到问题。我希望调用这样的东西 @post.club_membership.username
并且值 return 是 post 的关联用户名。如果我现在调用它,我会得到
NoMethodError: undefined method `username' for nil:NilClass
谢谢!
编辑--
这是 post table.
的创建迁移
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
更新答案
多亏了这个网站上很棒的用户,我才能够解决这个问题。
我的 Post 模型应该将此作为关联:
belongs_to :club_membership, :class_name => 'ClubMembership', :foreign_key => :member_id, :primary_key => :id
这让我可以调用 @post.club_membership.member.username
来获取要显示的 poster 的用户名。
正如您在(优秀的)问题中所展示的,您的 Post 具有以下属性(以及时间戳和 ID):
member_id: 29
club_id: 22
title: "fdsafsadfas"
postcontent: "fsdafsadfsadfsadfasdf"
您的 Post
模型具有以下关联:
belongs_to :club
belongs_to :club_membership
第一个将查找名为 club_id
的属性(并找到它),第二个将查找名为 club_membership_id
的属性但找不到。所以联想的方法永远行不通
根据您的代码和问题判断,我怀疑这就是您需要有人指出的全部内容,但如果您不清楚您的问题,请告诉我。
我一直在研究这个下午,我很接近,但我的大脑只是跳过一些简单的东西,但我看不到它。
在此站点上,用户可以创建和加入俱乐部。当用户加入俱乐部时,他们将作为俱乐部成员加入。俱乐部成员可以向俱乐部发出 posts。出于某种原因,我无法让 post 协会的俱乐部成员正常工作。该值正在用正确的 ID 填充,但是当我在控制台中检查关联时,我得到的是 nil。我的目标是能够获得创建 post.
的俱乐部成员的用户名用户模型
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
Post 型号
belongs_to :club
belongs_to :club_membership
相关路线
resources :clubs do
resources :posts
end
resources :club_memberships, :only => [:index, :create, :destroy]
Post控制器
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
post 是通过关注俱乐部页面上的 new_club_post_path(@club) link_to 创建的。 post 已正确创建且值正确。
最近 post
的控制台结果> @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 return是真的
> @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 所以我认为关联工作正常。我只是在从 post 到 club_membership 时遇到问题。我希望调用这样的东西 @post.club_membership.username
并且值 return 是 post 的关联用户名。如果我现在调用它,我会得到
NoMethodError: undefined method `username' for nil:NilClass
谢谢!
编辑-- 这是 post table.
的创建迁移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
更新答案 多亏了这个网站上很棒的用户,我才能够解决这个问题。
我的 Post 模型应该将此作为关联:
belongs_to :club_membership, :class_name => 'ClubMembership', :foreign_key => :member_id, :primary_key => :id
这让我可以调用 @post.club_membership.member.username
来获取要显示的 poster 的用户名。
正如您在(优秀的)问题中所展示的,您的 Post 具有以下属性(以及时间戳和 ID):
member_id: 29
club_id: 22
title: "fdsafsadfas"
postcontent: "fsdafsadfsadfsadfasdf"
您的 Post
模型具有以下关联:
belongs_to :club
belongs_to :club_membership
第一个将查找名为 club_id
的属性(并找到它),第二个将查找名为 club_membership_id
的属性但找不到。所以联想的方法永远行不通
根据您的代码和问题判断,我怀疑这就是您需要有人指出的全部内容,但如果您不清楚您的问题,请告诉我。