這裏是我的模型:如何從我的表得到Rails相關數據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如果你用模型的實際代碼替換你的課程的散文描述,那麼這個問題更好。 – Thilo 2011-05-01 19:35:06
編輯你的問題,並正確的結構。這很難理解 – 2011-05-01 20:34:50
@Thilo&@Jatin - 我編輯了我的問題。希望它更清楚。 – FattRyan 2011-05-01 21:53:35