2011-01-27 6 views
0

나는 rails3 + mongoid + devise를 사용하여 응용 프로그램을 만듭니다. 나는 사용자가 있고 각각의 사용자는 appontments를 가질 수있다. 나는 약속 문서에 user_id를 명시 적으로 저장해야하는지 또는 mongoid에게 정의 된 관계를 자동으로 처리하도록하는 방법에 대해 궁금하다. 내가 약속을 만들고 (CURRENT_USER 유증을 사용)는 현재 로그인 한 사용자와 관련된 문제에 대해 이동하는 방법에 대해 궁금몽고 이드 관계 연결

class User 
    include Mongoid::Document 
    devise: database_authenticable, :registerable, :recoverable, :rememberable, :trackable, :validatable 

    field :username 
    validates_presence_of :username 
    validates_uniqueness_of :name, :email, :case_sensitive => false 
    attr_accessible :name, :email, :password, :password_confirmation 

    references_many :appointments 
end 

class Appointment 
    include Mongoid::Document 

    field :date, :type => Date, :default => Date.today 
    referenced_in :user 
end 

을 다음과 같이

사용자와 약속 모델

입니다.

다음 workout_controller에 대한 조언, 특히 2 줄?

def create 
    @appointment = current_user.Appointment.new(params[:appointment]) 
    if @appointment.save 
    redirect_to(:action => 'show', :id => @appointment._id) 
    else 
    render('edit') 
    end 
end 

답변

1

첫째을, 나는 Appointment 클래스의 마지막 라인이 referenced_in :user 대신 :person 말을해야 생각합니다.

@appointment = current_user.appointments.new(params[:appointment]) 

을 저장 한 후, current_user.appointments 새로운 약속을 포함해야한다 : 다음과 같이

그런 다음 컨트롤러의 라인이 문제를 해결 할 수 있어야한다.

+0

대신 다음 사람이 있습니다. 사용자가 내 부분에 오타가 있습니다. 하지만, 네, 이제는 훌륭하게 작동합니다. "appointments.new"대신 "appointments.new"를 사용해야한다는 사실을 잊어 버렸습니다. – Hutch

관련 문제