IT

has_many : through를 사용하는 Rails 중첩 양식, 조인 모델의 속성을 편집하는 방법은 무엇입니까?

lottoking 2020. 8. 15. 09:29
반응형

has_many : through를 사용하는 Rails 중첩 양식, 조인 모델의 속성을 편집하는 방법은 무엇입니까?


accepts_nested_attributes_for를 사용할 때 조인 모델의 속성을 어떻게 편집합니까?

3 가지 모델이 있습니다 : 링커에 의해 결합 된 주제 및 가이드

class Topic < ActiveRecord::Base
  has_many :linkers
  has_many :articles, :through => :linkers, :foreign_key => :article_id
  accepts_nested_attributes_for :articles
end
class Article < ActiveRecord::Base
  has_many :linkers
  has_many :topics, :through => :linkers, :foreign_key => :topic_id
end
class Linker < ActiveRecord::Base
  #this is the join model, has extra attributes like "relevance"
  belongs_to :topic
  belongs_to :article
end

그래서 토픽 컨트롤러의 "new"액션으로 글을 찾을 때 ...

@topic.articles.build

... 토픽 /new.html.erb에 중첩 된 양식을 만듭니다 ...

<% form_for(@topic) do |topic_form| %>
  ...fields...
  <% topic_form.fields_for :articles do |article_form| %>
    ...fields...

... Rails는 자동으로 링커를 생성합니다. 이제 내 질문에 대해 : 내 링커 모델에는 "새 주제"에 대한 설명 수있는 속성도 있습니다. 그러나 Rails가 자동으로 생성하는 링커는 topic_id 및 article_id를 모든 속성에 대해 nil 값을 갖습니다. 다른 링커 속성에 대한 필드를 "new topic"으로 넣어서 nil이 나오지 않게해야해야합니까?


답을 찾았습니다. 트릭은 다음과 가변합니다.

@topic.linkers.build.build_article

그럼 링커가 빌드 된 다음 각 링커에 대한 아티클이 빌드됩니다. 그래서, 모델 :
topic.rb 요구 accepts_nested_attributes_for :linkers
linker.rb 요구accepts_nested_attributes_for :article

그런 다음 형식 :

<%= form_for(@topic) do |topic_form| %>
  ...fields...
  <%= topic_form.fields_for :linkers do |linker_form| %>
    ...linker fields...
    <%= linker_form.fields_for :article do |article_form| %>
      ...article fields...

레일에 의해 생성 된 양식이 레일에 생성 될 때 controller#action의가 params(일부 추가 속성으로 구성)

params = {
  "topic" => {
    "name"                => "Ruby on Rails' Nested Attributes",
    "linkers_attributes"  => {
      "0" => {
        "is_active"           => false,
        "article_attributes"  => {
          "title"       => "Deeply Nested Attributes",
          "description" => "How Ruby on Rails implements nested attributes."
        }
      }
    }
  }
}

공지 사항에서는 linkers_attributes제로 고급입니다 Hash와 키 String, 아닌가 Array? 이는 서버로 전송되는 양식 필드 키가 다음과 같기 때문입니다.

topic[name]
topic[linkers_attributes][0][is_active]
topic[linkers_attributes][0][article_attributes][title]

레코드 생성은 이제 다음과 같이 간단합니다.

TopicController < ApplicationController
  def create
    @topic = Topic.create!(params[:topic])
  end
end

솔루션에서 has_one을 사용할 때 빠른 GOTCHA. 난 그냥 사용자에 의해 주어진 답 붙여 복사합니다 KandadaBoggu 에서 이 스레드를 .


build메소드 서명은 다릅니다 has_onehas_many협회.

class User < ActiveRecord::Base
  has_one :profile
  has_many :messages
end

연결을위한 빌드 구문 has_many:

user.messages.build

연결을위한 빌드 구문 has_one:

user.build_profile  # this will work

user.profile.build  # this will throw error

자세한 내용 has_one연관 문서읽어보십시오 .

참고 URL : https://stackoverflow.com/questions/2182428/rails-nested-form-with-has-many-through-how-to-edit-attributes-of-join-model

반응형