2
我有三個模型Role
,Action
和RoleAction
一些代碼:Rails的HAS_MANY通過條件方面
class Role < ActiveRecord::Base
has_many :users
has_many :actions, -> {where role_actions:{status: 1}}, :through => :roleActions
has_many :actions, :through => :roleActions #other context(manager, etc...)
has_many :roleActions
end
class Action < ActiveRecord::Base
has_many :actions, foreign_key: 'parent_id'
has_many :roleActions
has_many :roles, through: :roleActions
end
class RoleAction < ActiveRecord::Base
belongs_to :role
belongs_to :action
end
當我使用role.actions
將得到role
行動,並在role_actions status == 1
。
但我想當我使用role.actions("manager")
(與「經理」是上下文名稱)將返回角色的所有行動。
我該怎麼辦?
謝謝!
http://stackoverflow.com/questions/408872/ rails-has-many-through-find-by-extra-attributes-in-join-model – lokson
你可以用'role.actions.where(name_of_attribute:'manager')'來實現。其中name_of_attribute是存儲「manager」值的屬性名稱 –