0
可以说我拥有has_many玩具的Person。然后我有一个玩具,它有许多颜色。我想在我的Person#show方法中做的是过滤包含一系列颜色的玩具。通过has_many关联中的属性值范围过滤ActiveRecord
class Person < ActiveRecord::Base
attr_accessible :name
has_many :toys
end
class Toy < ActiveRecord::Base
attr_accessible :size
belongs_to :person
has_many :colors
end
class Color < ActiveRecord::Base
attr_accessible :color
belongs_to :toy
end
然后在我的PersonController中,我想过滤玩具是一系列的颜色。
class PersonController < ApplicationController
def show
@person = Person.find(params[:id])
# Now I want to filter by toy colors that might be red or blue or purple or etc...
# So when in my view I do @person.toys I know they only contain the filtered colors
@person.toys.categories
end
end
帮助或建议将不胜感激。仍然积极学习Rails。