has_one : through 및 has_many : through

user3269780
rails -v = 4.0
ruby -v = 2.1.1

has_one : through에 심각한 문제가 있습니다. 모든 Google 1st 2 페이지 링크는 파란색으로 표시됩니다 (모두 살펴 보았습니다).

내 문제는 내가하려고 할 때

post = Post.last
post.build_user

이 말을 '정의되지 않은 메서드`build_user . 협회가있는 수업은 다음과 같습니다.

class Post < ActiveRecord::Base    
    has_one :user_post
    has_one :user, class_name: "User", through: :user_post

   accepts_nested_attributes_for :user
end

class UserPost < ActiveRecord::Base
    belongs_to :user
    belongs_to :post
end

class User < ActiveRecord::Base
    has_many :user_posts
    has_many :posts, through: :user_posts 
end

누군가이 문제를 해결하는 데 도움을 주시면 정말 좋을 것입니다.

감사합니다.

키르 티 토랏

당신은 설정 A를 시도하는 Many-to-Many Relationship사이에 Post하고 User있지만, 현재의 설정이 올바르지 않습니다.

당신은 사용해야하는 has_many대신 has_onePost모델.

class Post < ActiveRecord::Base    
  has_many :user_posts
  has_many :users, through: :user_posts
end

그런 다음 사용자를 다음과 같이 빌드 할 수 있습니다.

post = Post.last
post.users.build

최신 정보

undefined methodbuild_user ' 로 오류가 발생 합니다. because you can only usepost.build_user if association betweenPost andUser ishas_one`이며 아래와 같이 정의됩니다.

class Post < ActiveRecord::Base
  has_one :user
end
class User < ActiveRecord::Base
  belongs_to :post    # foreign key - post_id
end

업데이트 2

또한 논리적으로 A user has_many posts AND A post has one User설정은 이상적으로

class Post < ActiveRecord::Base
  belongs_to :user   # foreign key - user_id
end
class User < ActiveRecord::Base
  has_many :posts    
end

그런 다음 사용자에 대한 게시물을 다음과 같이 작성할 수 있습니다.

user = User.last
user.posts.build

게시물에 대한 사용자를 구축하려면 :

post = Post.last
post.build_user

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

has_many 후 has_many : through?

분류에서Dev

레일 has_one through form

분류에서Dev

link to relationship name in has_many :through

분류에서Dev

has_many : through 양식

분류에서Dev

Rails has_many : through with the where 절

분류에서Dev

Finding Users with has_many :through

분류에서Dev

Make has_many :through not deleteable

분류에서Dev

Join has_many :through attributes

분류에서Dev

has_many / : through rails4

분류에서Dev

Rails has_many : through, undefined method

분류에서Dev

레일 has_many 및 has_one

분류에서Dev

has_many 및 has_one 관계 SilverStripe

분류에서Dev

has_many : through in Rails로 생성 및 업데이트

분류에서Dev

Rails 4 : : has_many, : through 및 테이블 이름

분류에서Dev

Rails 4 has_one through with where 절

분류에서Dev

ActiveAdmin 필터 : has_many : through 속성

분류에서Dev

Can a has_many :through association have other associations?

분류에서Dev

Rails 3: has_many through controller action

분류에서Dev

Creating multiple has_many :through records with form_for

분류에서Dev

has_many : through로 사용자 찾기

분류에서Dev

NoMethodError: undefined method `user' for has_many :through Association

분류에서Dev

ActiveRecord, Rails 4 : has_many : through with scoped conditions failure

분류에서Dev

has_many through가있는 곳

분류에서Dev

Ruby on Rails has_many : through in a polymorphic association

분류에서Dev

rails cancancan cancan has_many through abilities

분류에서Dev

has_many through-속성 액세스

분류에서Dev

has_many : through with class_name 및 foreign_key가 작동하지 않음

분류에서Dev

has_many through 및 join 테이블이있는 간단한 양식

분류에서Dev

has_many through : in Rails를 사용하여 현재 및 이전 관계 쿼리

Related 관련 기사

뜨겁다태그

보관