0

이 코드를 사용하여 오류가 계속 발생합니다. 오류가 오류 메시지가 내가 대신 "사용자"의 "사용자"를 사용하지만 난 그렇게 UNIQ 사용할 수 없습니다 제안 발생했지만 왜# <Post : 0xaa09664>에 대한 정의되지 않은 메소드

def create 
    new_post 
    @post.save! 

    (@post.users.uniq - [current_user]).each do |user| 
    Notification.create(recipient: user, actor:current_user, action: "posted", notifiable: @post) 
    end 

redirect_to post_index_path 

end 

나는 확실하지 않다. 나는 또한 그것을하는 더 많은 오류가 발생합니다. 편집

: 나는 또한 주제 모델이

class Post < ActiveRecord::Base 
    has_many :comments 
    belongs_to :user 
    belongs_to :subject 
    validates_presence_of :content 
end 

: 여기 내 게시물 모델입니다

class Subject < ActiveRecord::Base 
    has_many :posts 
    has_many :users, through: :posts 
    validates_presence_of :name 
    validates_uniqueness_of :name 
end 

사용자 모델 :

class User < ActiveRecord::Base 

    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 

    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 
    validates :name, presence: true 

    has_many :listings, dependent: :destroy 

    has_many :purchasing, class_name: "Transaction", foreign_key: "buyer_id", dependent: :destroy 
    has_many :sell, class_name: "Transaction", foreign_key: "seller_id", dependent: :destroy 


    has_many :purchased, class_name: "Archive", foreign_key: "buyer_id", dependent: :destroy 
    has_many :sales, class_name: "Archive", foreign_key: "seller_id", dependent: :destroy 

    has_many :selling_rooms, class_name: "Room", foreign_key: "seller_id", dependent: :destroy 
    has_many :buying_room, class_name: "Room", foreign_key: "buyer_id", dependent: :destroy 

    has_many :comments, through: :posts 
    has_many :posts 

    has_many :notifications, foreign_key: :recipient_id 


    def can_buy?(listing_price) 
    if self.points >= listing_price 
     true 
    else 
     false 
    end 
    end 

    def withdraw(listing_price) 
    self.points -= listing_price 
    end 


    def purchasing_list 
    purchasing.includes(:seller, :listing) 
    end 

    def purchased_list 
    purchased.includes(:seller, :listing) 
    end 

    def sell_list 
    sell.includes(:seller, :listing) 
    end 

    def sales_list 
    sales.includes(:seller, :listing) 
    end 
end 
+0

에서 (user.rb) 사용자 모델을 가지고 의미가? 게시물은 애플리케이션에서 다른 것을 사용하지 않는 한 논리적으로 '사용자'에 속합니다. 그래서, 당신은 게시물 개체'사용자'메서드를 호출 할 수 없습니다 –

+0

안녕하세요, 그리고 스택 오버플로 환영합니다. 이 코드와 관련된 코드를 제공 할 수 있습니까? 예를 들어 게시물은 사용자와 어떤 관련이 있습니까? 게시물에 둘 이상의 사용자가 있거나 하나만 있습니까? 전체 오류 메시지를 표시 할 수 있습니까? 서버 로그 (일반적으로'log/development.log' 또는 콘솔 화면)를보고 오류 메시지 직후에 오는 3 ~ 4 줄의 파일 이름을 표시 할 수 있습니까? (이것들은 에러를 던지는 정확한 코드 라인을 찾아내는 데 도움이됩니다). –

+0

포스트 및 사용자 모델을 포함하십시오 (관련 부분 이상) –

답변

1

난 당신이하지 않았다 추측하고있다 관계를 옳은 길로 선언하십시오. ?

has_one :user 

을 또는 당신은 첫 번째 경우 현재 동작이 예상된다

has_many :users 

를 선언 않았다 당신이 당신의 포스트 모델이 있습니까. 원하는 상황의 경우, 논리적으로

has_many :posts 

당신이 달성하려고하는 무엇을 당신의 포스트 모델

belongs_to :user 
+0

편집에 표시된 것과 같이 연결된 주체 모델에 has_many : users가 있습니다. – CJK

관련 문제