3 내 모델은 다음과 같습니다 여기 어떻게 레일에 내 테이블에서 특정 데이터를 얻기 위해 여기
class Food < ActiveRecord::Base
has_many :lists
has_many :costs, :through => :lists
end
class List < ActiveRecord::Base #each food can have multiple lists, ordered by date
belongs_to :food
has_many :costs, :dependent => :destroy
accetps_nested_attribute_for :costs, :allow_destroy => true
end
class Cost < ActiveRecord::Base #costs are nested inside of a list
belongs_to :food
belongs_to :list
end
내 스키마 (당신이 볼 필요가있는 부분)입니다 : 내가 원하는 무엇
create_table "foods", :force => true do |t|
t.integer "food_id"
t.string "name"
t.string "type" # this is where I can choose a 'fruit' or a 'vegetable'
end
create_table "lists", :force => true do |t|
t.integer "food_id"
t.integer "quarter" #this is how lists are ordered
t.integer "year"
end
create_table "costs", :force => true do |t|
t.integer "amount"
t.integer "list_id"
t.integer "food_id"
end
내 테이블을 통해 특정 기준에 따라 총 비용 또는 평균 비용을 표시 할 수 있습니다. 예를 들어 특정 기간 동안 모든 과일의 총 또는 평균 비용 (비용 모델의 금액 속성)을 알고 싶으면 (분기 및 연도별로 목록 모델에서 정렬 됨). 그게 더 명확한가요? 지금까지 의견을 보내 주셔서 감사합니다.
를이 q를 이해하는 데 크게 도움이 될 것입니다. 클래스의 산문 설명을 모델의 실제 코드로 바꾼다면 uestion이 더 좋습니다. – Thilo
질문을 편집하고 올바르게 구성하십시오. 이 방법을 이해하기는 너무 어렵습니다. –
@Thilo & @Jatin - 제 질문을 편집했습니다. 바라기를 바란다. – FattRyan