【问题标题】:Creating a Nested Association with Has_Many :Through使用 Has_Many 创建嵌套关联:Through
【发布时间】:2015-09-03 22:48:53
【问题描述】:

我不确定我的标题是否正确使用了词汇。我发现了一些人们遇到类似问题的 SO 帖子(我在这些帖子中找不到解决方案),所以我使用了与那些类似的标题。我正在尝试允许用户创建俱乐部并自动将用户分配为俱乐部的成员。但是,当我尝试创建俱乐部时,似乎出现了问题。

我有三个模型:

###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"

最后,这是日志在帖子中显示的内容

#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 中发生了什么,也不知道接下来从哪里解决这个问题。似乎我想要的参数正在通过,但实际上并没有。任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 nested-attributes has-many-through


    【解决方案1】:

    我想通了。我一次尝试了两件事,它奏效了,所以我不能确定是什么有帮助,但我很确定第二件事是最重要的。

    首先,我从两个模型(Club 和 ClubMembership)中删除了 attr_accessor。为什么我一开始就把它放在那里?不知道。我可能在某个地方看到它并认为它很酷。

    其次,同样在模型中,我没有发布验证器,因为我认为它们不重要。我有:

    validates :club_name, :club_type, :presence => true validates :club_name, :uniqueness => true

    我把它改成了:

    validates :club_name, presence: true 
    validates :club_type, presence: true
    

    事实证明这很重要,现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-16
      • 2014-06-03
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多