在 Rails 中通过 has_many :through 中的中间属性查找
我的模型中有以下多对多设置:
class Project < ActiveRecord::Base
has_many :projectcollaborations
has_many :partners, :through => :projectcollaborations, :source => :partner
end
class Partner < ActiveRecord::Base
has_many :projectcollaborations
has_many :projects, :through => :projectcollaborations, :source => :project
end
class Projectcollaboration < ActiveRecord::Base
belongs_to :project
belongs_to :partner
end
我可以访问:
@partner = Partner.first
@partner.projects
@partner.projectcollaborations.find_by_myrole('creator')
....
现在我如何访问 @partner
的所有项目,其中有 myrole 创建者多对多关系表?
Possible Duplicate:
Rails has_many :through Find by Extra Attributes in Join Model
I have the following many to many setup in my model:
class Project < ActiveRecord::Base
has_many :projectcollaborations
has_many :partners, :through => :projectcollaborations, :source => :partner
end
class Partner < ActiveRecord::Base
has_many :projectcollaborations
has_many :projects, :through => :projectcollaborations, :source => :project
end
class Projectcollaboration < ActiveRecord::Base
belongs_to :project
belongs_to :partner
end
I can access:
@partner = Partner.first
@partner.projects
@partner.projectcollaborations.find_by_myrole('creator')
....
now how can I access the @partner
's all project having myrole creator in my many-to-many relationship table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许还有另一种更漂亮、更铁路的方式,但这就是我要做的
maybe there is another, prettier, railsier way, but this is how i'd do it