使用 Has_Many 创建嵌套关联 :Through
Creating a Nested Association with Has_Many :Through
我不确定我的标题是否正确使用了词汇。我发现了一些 SO posts,其中人们有类似的问题(我在那些 posts 中找不到解决方案),所以我使用了与这些类似的标题。我正在尝试允许用户创建俱乐部并自动将用户指定为俱乐部成员。但是,当我尝试创建俱乐部时,似乎出现了一些问题。
我有三个模型:
###Models###
#user
has_many :club_memberships, :class_name => 'ClubMembership', :foreign_key => :member_id, :primary_key => :id
has_many :clubs, :through => :club_memberships
#club
attr_accessor :club_name, :club_type
has_many :club_memberships
has_many :members, :through => :club_memberships
#clubmembership
attr_accessor :club_id, :member_id, :membership_type
belongs_to :club
belongs_to :member, :class_name => "User", :foreign_key => :member_id, :primary_key => :id
这里是控制器的相关部分
###Controllers###
#Clubs
def new
@club = Club.new
end
def create
@club = Club.new(club_params)
if @club.save
@club_membership = ClubMembership.create(
member_id: current_user.id,
club_id: @club.id,
membership_type: 'founder'
)
flash[:success] = "Club Created!"
redirect_to root_url
else
render @club.errors.full_messages
end
end
private
def club_params
params.require(:club).permit(:club_name, :club_type)
end
#ClubMemberships
def create
@club_membership = ClubMembership.new(club_membership_params)
if @club_membership.save
render @club_membership
else
render @club_membership.errors.full_messages
end
end
private
def club_membership_params
params.require(:club_membership).permit(:member_id, :club_id, :membership_type)
end
我的form_for
###View###
#club#new
= form_for(@club) do |f|
.field.center-align
= f.label :club_name
= f.text_field :club_name, :class => "form-control fieldbox", autofocus: true
.field.center-align
= f.label :club_type
= f.text_field :club_type, :class => 'form-control fieldbox', autofocus: true
.actions.center-align
= f.submit "Create Club!", :class => "btn hoverable padtop"
最后,这是日志在 post
上显示的内容
#log
Started POST "/clubs" for at 2015-09-03 22:32:41 +0000
Cannot render console from ! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ClubsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ar2dv41/Tqk9EVjwfLLeD8bnpLoVWQIdDxG3Ju1GO3stLLvPd/FFgoFF9YuHobWbgb2byqkgAMiWRJAg5YcGKQ==", "club"=>{"club_name"=>"Test Club", "club_type"=>"Test Type"}, "commit"=>"Create Club!"}
(0.2ms) BEGIN
Club Exists (0.4ms) SELECT 1 AS one FROM `clubs` WHERE `clubs`.`club_name` = BINARY 'Test Club' LIMIT 1
SQL (0.3ms) INSERT INTO `clubs` (`created_at`, `updated_at`) VALUES ('2015-09-03 22:32:41', '2015-09-03 22:32:41')
(3.4ms) COMMIT
User Load (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 56 LIMIT 1
(0.1ms) BEGIN
ClubMembership Exists (0.3ms) SELECT 1 AS one FROM `club_memberships` WHERE (`club_memberships`.`member_id` = BINARY 56 AND `club_memberships`.`club_id` IS NULL) LIMIT 1
SQL (1.6ms) INSERT INTO `club_memberships` (`created_at`, `updated_at`) VALUES ('2015-09-03 22:32:41', '2015-09-03 22:32:41')
(3.1ms) COMMIT
Redirected to c9
Completed 302 Found in 71ms (ActiveRecord: 11.1ms)
我会说实话。我不知道 POST 中发生了什么,也不知道下一步该从哪里解决这个问题。似乎我想要的参数正在通过,但后来却没有。任何帮助将不胜感激。
我明白了。我同时尝试了两件事并且都奏效了,所以我不能确定有什么帮助,但我很确定第二件事是最重要的。
首先,我从两个模型(Club 和 ClubMembership)中删除了 attr_accessor
。为什么我首先把它放在那里?不知道。我可能在哪里看到过,觉得很酷。
其次,同样在模型中,我有我没有的验证器 post 因为我认为它们不重要。我有:
validates :club_name, :club_type, :presence => true
validates :club_name, :uniqueness => true
我将其更改为:
validates :club_name, presence: true
validates :club_type, presence: true
事实证明这很重要,现在一切正常。
我不确定我的标题是否正确使用了词汇。我发现了一些 SO posts,其中人们有类似的问题(我在那些 posts 中找不到解决方案),所以我使用了与这些类似的标题。我正在尝试允许用户创建俱乐部并自动将用户指定为俱乐部成员。但是,当我尝试创建俱乐部时,似乎出现了一些问题。
我有三个模型:
###Models###
#user
has_many :club_memberships, :class_name => 'ClubMembership', :foreign_key => :member_id, :primary_key => :id
has_many :clubs, :through => :club_memberships
#club
attr_accessor :club_name, :club_type
has_many :club_memberships
has_many :members, :through => :club_memberships
#clubmembership
attr_accessor :club_id, :member_id, :membership_type
belongs_to :club
belongs_to :member, :class_name => "User", :foreign_key => :member_id, :primary_key => :id
这里是控制器的相关部分
###Controllers###
#Clubs
def new
@club = Club.new
end
def create
@club = Club.new(club_params)
if @club.save
@club_membership = ClubMembership.create(
member_id: current_user.id,
club_id: @club.id,
membership_type: 'founder'
)
flash[:success] = "Club Created!"
redirect_to root_url
else
render @club.errors.full_messages
end
end
private
def club_params
params.require(:club).permit(:club_name, :club_type)
end
#ClubMemberships
def create
@club_membership = ClubMembership.new(club_membership_params)
if @club_membership.save
render @club_membership
else
render @club_membership.errors.full_messages
end
end
private
def club_membership_params
params.require(:club_membership).permit(:member_id, :club_id, :membership_type)
end
我的form_for
###View###
#club#new
= form_for(@club) do |f|
.field.center-align
= f.label :club_name
= f.text_field :club_name, :class => "form-control fieldbox", autofocus: true
.field.center-align
= f.label :club_type
= f.text_field :club_type, :class => 'form-control fieldbox', autofocus: true
.actions.center-align
= f.submit "Create Club!", :class => "btn hoverable padtop"
最后,这是日志在 post
上显示的内容#log
Started POST "/clubs" for at 2015-09-03 22:32:41 +0000
Cannot render console from ! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ClubsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ar2dv41/Tqk9EVjwfLLeD8bnpLoVWQIdDxG3Ju1GO3stLLvPd/FFgoFF9YuHobWbgb2byqkgAMiWRJAg5YcGKQ==", "club"=>{"club_name"=>"Test Club", "club_type"=>"Test Type"}, "commit"=>"Create Club!"}
(0.2ms) BEGIN
Club Exists (0.4ms) SELECT 1 AS one FROM `clubs` WHERE `clubs`.`club_name` = BINARY 'Test Club' LIMIT 1
SQL (0.3ms) INSERT INTO `clubs` (`created_at`, `updated_at`) VALUES ('2015-09-03 22:32:41', '2015-09-03 22:32:41')
(3.4ms) COMMIT
User Load (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 56 LIMIT 1
(0.1ms) BEGIN
ClubMembership Exists (0.3ms) SELECT 1 AS one FROM `club_memberships` WHERE (`club_memberships`.`member_id` = BINARY 56 AND `club_memberships`.`club_id` IS NULL) LIMIT 1
SQL (1.6ms) INSERT INTO `club_memberships` (`created_at`, `updated_at`) VALUES ('2015-09-03 22:32:41', '2015-09-03 22:32:41')
(3.1ms) COMMIT
Redirected to c9
Completed 302 Found in 71ms (ActiveRecord: 11.1ms)
我会说实话。我不知道 POST 中发生了什么,也不知道下一步该从哪里解决这个问题。似乎我想要的参数正在通过,但后来却没有。任何帮助将不胜感激。
我明白了。我同时尝试了两件事并且都奏效了,所以我不能确定有什么帮助,但我很确定第二件事是最重要的。
首先,我从两个模型(Club 和 ClubMembership)中删除了 attr_accessor
。为什么我首先把它放在那里?不知道。我可能在哪里看到过,觉得很酷。
其次,同样在模型中,我有我没有的验证器 post 因为我认为它们不重要。我有:
validates :club_name, :club_type, :presence => true
validates :club_name, :uniqueness => true
我将其更改为:
validates :club_name, presence: true
validates :club_type, presence: true
事实证明这很重要,现在一切正常。