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”值的属性名称 –