2013-11-11 2 views
0

특정 날짜에 event이 발생하는지 확인하는 쿼리를 작성하는 데 도움이 필요합니다.has_many 관계에서 자식 특성 가져 오기

event has_many daysday 내가 접근을 시도하고있다 :date이라는 속성을 가지고 있지만 그것을 얻을 수없는 것.

코드 : 내보기에서

class Event < ActiveRecord::Base 
    attr_accessible :title, :days_attributes 

    has_many :days 
    accepts_nested_attributes_for :days 

    scope :occurs_on, lambda {|date| where(Day.first.date => date)} 
end 

class Day < ActiveRecord::Base 
    attr_accessible :date, :event_id 
    belongs_to :event 

    validates :date, presence: true 
end 

class EventsController < ApplicationController 
    def index 
    @events = Event.occurs_on(Date.today) 
    end 
end 

내가 이런 식으로 날짜를 표시 할 수는 : <%= event.days.first.date.strftime("%A, %B %e") %> 그래서 나는 내 모델에서 일 거라고 생각하지만 그렇지 않습니다. undefined method 'to_sym' 오류가 발생합니다.

감사합니다. 내가 제대로 질문을 읽고 있어요 경우

답변

0

, 나는 당신이 당신의 범위에 join을 수행 할 것 같아요 :

class Event < ActiveRecord::Base 
    attr_accessible :title, :days_attributes 

    has_many :days 
    accepts_nested_attributes_for :days 

    scope :occurs_on, lambda {|date| joins(:days).where("days.date = ?", date)} 
end