我在正确设置模型关系时遇到问题。我有以下课程:
个人投资组合.rb
class PersonalPortfolio < Portfolio
has_many :user_stake_in_portfolios, :foreign_key => :possession_id
belongs_to :owner, :class_name => 'User'
has_many :managers, :through => :user_stake_in_portfolios, :source => :user
has_many :company_permissions, :foreign_key => :owner_id
has_many :company_permissions_as_viewer, :class_name => 'CompanyPermission', :foreign_key => :owner_id, :conditions => 'can_view_info = true || can_edit_cap_table = true || can_view_key_metric = true || can_view_all_metrics = true || can_view_cap_table = true || can_edit_managers = true || can_edit_info = true || can_edit_metrics = true'
has_many :companies_as_viewer, :class_name => 'Company', :through => :company_permissions_as_viewer, :source => :entity
has_many :company_permissions_as_manager, :class_name => 'CompanyPermission', :foreign_key => :owner_id, :conditions => { :can_edit_managers => true }
has_many :companies_as_manager, :class_name => 'Company', :through => :company_permissions_as_manager, :source => :entity
has_many :portfolio_permissions, :foreign_key => :owner_id
has_many :portfolio_permissions_as_viewer, :class_name => 'PortfolioPermission', :foreign_key => :owner_id, :conditions => 'can_view_info = true || can_edit_cap_table = true || can_view_key_metric = true || can_view_all_metrics = true || can_view_cap_table = true || can_edit_managers = true || can_edit_info = true || can_edit_metrics = true'
has_many :portfolios_as_viewer, :class_name => 'Portfolio', :through => :portfolio_permissions_as_viewer, :source => :entity
has_many :portfolio_permissions_as_manager, :class_name => 'PortfolioPermission', :foreign_key => :owner_id, :conditions => { :can_edit_managers => true }
has_many :portfolios_as_manager, :class_name => 'GroupPortfolio', :through => :portfolio_permissions_as_manager, :source => :entity
after_create :add_manager
...
end
用户.rb
class User < ActiveRecord::Base
...
has_many :user_stake_in_portfolios, foreign_key: :owner_id
has_many :portfolios, through: :user_stake_in_portfolios
has_many :personal_portfolios, through: :user_stake_in_portfolios, :class_name => 'PersonalPortfolio', :source => :portfolio, :conditions => { :type => 'PersonalPortfolio' }
has_many :companies, through: :portfolios, source: :portfolio_companies, class_name: 'Company'
has_many :managed_portfolios, :through => :personal_portfolios, :source => :portfolios_as_manager
has_many :viewable_portfolios, :through => :personal_portfolios, :source => :portfolios_as_viewer
has_many :managed_companies, :through => :personal_portfolios, :source => :companies_as_manager
has_many :viewable_companies, :through => :personal_portfolios, :source => :companies_as_viewer
...
end
奇怪的是,如果我运行命令,User.find(3).personal_portfolios.first.portfolios_as_viewer
我会得到我期望的结果(3 个投资组合)。但问题是当我尝试时User.find(3).viewable_portfolios
,我只得到 1 个投资组合(这是三个投资组合中的一个)。用户。viewable_portfolios
是作为源的has_many :through
使用。PersonalPortfolio.portfolios_as_viewer
查看第二个命令生成的查询时,您可以看到它portfolios
两次使用该表:
SELECT `portfolios`.*
FROM `portfolios`
INNER JOIN `permissions`
ON `portfolios`.`id` = `permissions`.`entity_id`
INNER JOIN `portfolios` `personal_portfolios_viewable_portfolios_join`
ON `permissions`.`owner_id` =
`personal_portfolios_viewable_portfolios_join`.`id`
INNER JOIN `stakes`
ON `personal_portfolios_viewable_portfolios_join`.`id` =
`stakes`.`possession_id`
WHERE `permissions`.`type` IN ( 'PortfolioPermission' )
AND `portfolios`.`type` IN ( 'PersonalPortfolio' )
AND `stakes`.`type` IN ( 'UserStakeInPortfolio' )
AND `stakes`.`owner_id` = 3
AND ( can_view_info = true
|| can_edit_cap_table = true
|| can_view_key_metric = true
|| can_view_all_metrics = true
|| can_view_cap_table = true
|| can_edit_managers = true
|| can_edit_info = true
|| can_edit_metrics = true )
AND ( `personal_portfolios_viewable_portfolios_join`.`type` = 'PersonalPortfolio' )
这对我来说很奇怪,因为User.find(3).managed_portfolios == User.find(3).personal_portfolios.first.portfolios_as_manager
返回 true 而User.find(3).viewable_portfolios == User.find(3).personal_portfolios.first.portfolios_as_viewer
返回 false。尽管关系看起来是一样的,但公司工作得很好(尽管由于依赖关系/数据不同,这并不能表明正确的关系)。
问题
我怎样才能得到User.find(3).viewable_portfolios
相同的回报User.find(3).personal_portfolios.first.portfolios_as_viewer
?
有用的输出
1.9.3-p362 :122 > User.find(3).viewable_portfolios
User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1
Portfolio Load (1.1ms) SELECT `portfolios`.* FROM `portfolios` INNER JOIN `permissions` ON `portfolios`.`id` = `permissions`.`entity_id` INNER JOIN `portfolios` `personal_portfolios_viewable_portfolios_join` ON `permissions`.`owner_id` = `personal_portfolios_viewable_portfolios_join`.`id` INNER JOIN `stakes` ON `personal_portfolios_viewable_portfolios_join`.`id` = `stakes`.`possession_id` WHERE `permissions`.`type` IN ('PortfolioPermission') AND `portfolios`.`type` IN ('PersonalPortfolio') AND `stakes`.`type` IN ('UserStakeInPortfolio') AND `stakes`.`owner_id` = 3 AND (can_view_info = true || can_edit_cap_table = true || can_view_key_metric = true || can_view_all_metrics = true || can_view_cap_table = true || can_edit_managers = true || can_edit_info = true || can_edit_metrics = true) AND (`personal_portfolios_viewable_portfolios_join`.`type` = 'PersonalPortfolio')
=> [#<PersonalPortfolio id: 3, name: "Manager Man's portfolio", established: "2013-09-24", created_at: "2013-09-24 23:20:34", updated_at: "2013-09-24 23:20:34", type: "PersonalPortfolio", base_capital_invested: nil, total_shares: nil, logo_file_name: nil, logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil, city: nil, state: nil, invited_manager_email: nil, claim_code: nil, angellist_url: nil, facebook_url: nil, linkedin_url: nil, twitter_url: nil, website_url: nil, longitude: nil, latitude: nil, is_single_use_fund: false, tagline: nil, thumb_file_name: nil, thumb_content_type: nil, thumb_file_size: nil, thumb_updated_at: nil, use_committed_capital: true, owner_id: 3>]
1.9.3-p362 :123 > User.find(3).viewable_portfolios.count
User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1
(2.2ms) SELECT COUNT(*) FROM `portfolios` INNER JOIN `permissions` ON `portfolios`.`id` = `permissions`.`entity_id` INNER JOIN `portfolios` `personal_portfolios_viewable_portfolios_join` ON `permissions`.`owner_id` = `personal_portfolios_viewable_portfolios_join`.`id` INNER JOIN `stakes` ON `personal_portfolios_viewable_portfolios_join`.`id` = `stakes`.`possession_id` WHERE `permissions`.`type` IN ('PortfolioPermission') AND `portfolios`.`type` IN ('PersonalPortfolio') AND `stakes`.`type` IN ('UserStakeInPortfolio') AND `stakes`.`owner_id` = 3 AND (can_view_info = true || can_edit_cap_table = true || can_view_key_metric = true || can_view_all_metrics = true || can_view_cap_table = true || can_edit_managers = true || can_edit_info = true || can_edit_metrics = true) AND (`personal_portfolios_viewable_portfolios_join`.`type` = 'PersonalPortfolio')
=> 1
1.9.3-p362 :124 > User.find(3).personal_portfolios.first.portfolios_as_viewer
User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1
PersonalPortfolio Load (0.8ms) SELECT `portfolios`.* FROM `portfolios` INNER JOIN `stakes` ON `portfolios`.`id` = `stakes`.`possession_id` WHERE `stakes`.`type` IN ('UserStakeInPortfolio') AND `portfolios`.`type` IN ('PersonalPortfolio') AND `stakes`.`owner_id` = 3 AND (`portfolios`.`type` = 'PersonalPortfolio') LIMIT 1
Portfolio Load (1.0ms) SELECT `portfolios`.* FROM `portfolios` INNER JOIN `permissions` ON `portfolios`.`id` = `permissions`.`entity_id` WHERE `permissions`.`type` IN ('PortfolioPermission') AND `permissions`.`owner_id` = 3 AND (can_view_info = true || can_edit_cap_table = true || can_view_key_metric = true || can_view_all_metrics = true || can_view_cap_table = true || can_edit_managers = true || can_edit_info = true || can_edit_metrics = true)
=> [#<PersonalPortfolio id: 3, name: "Manager Man's portfolio", established: "2013-09-24", created_at: "2013-09-24 23:20:34", updated_at: "2013-09-24 23:20:34", type: "PersonalPortfolio", base_capital_invested: nil, total_shares: nil, logo_file_name: nil, logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil, city: nil, state: nil, invited_manager_email: nil, claim_code: nil, angellist_url: nil, facebook_url: nil, linkedin_url: nil, twitter_url: nil, website_url: nil, longitude: nil, latitude: nil, is_single_use_fund: false, tagline: nil, thumb_file_name: nil, thumb_content_type: nil, thumb_file_size: nil, thumb_updated_at: nil, use_committed_capital: true, owner_id: 3>, #<GroupPortfolio id: 5, name: "New Fund, LLC", established: "2009-01-01", created_at: "2013-09-24 23:20:36", updated_at: "2013-09-24 23:20:36", type: "GroupPortfolio", base_capital_invested: 2000000, total_shares: 1000000, logo_file_name: nil, logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil, city: "Hamlin", state: "WV", invited_manager_email: nil, claim_code: nil, angellist_url: nil, facebook_url: nil, linkedin_url: nil, twitter_url: nil, website_url: nil, longitude: -82.1029, latitude: 38.2787, is_single_use_fund: false, tagline: nil, thumb_file_name: nil, thumb_content_type: nil, thumb_file_size: nil, thumb_updated_at: nil, use_committed_capital: false, owner_id: nil>, #<GroupPortfolio id: 6, name: "SB, LLC", established: "2009-01-01", created_at: "2013-09-24 23:20:36", updated_at: "2013-09-24 23:20:36", type: "GroupPortfolio", base_capital_invested: 2000000, total_shares: 2000000, logo_file_name: nil, logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil, city: "Bloomington", state: "IN", invited_manager_email: nil, claim_code: nil, angellist_url: nil, facebook_url: nil, linkedin_url: nil, twitter_url: nil, website_url: nil, longitude: -86.5264, latitude: 39.1653, is_single_use_fund: false, tagline: nil, thumb_file_name: nil, thumb_content_type: nil, thumb_file_size: nil, thumb_updated_at: nil, use_committed_capital: true, owner_id: nil>]
1.9.3-p362 :125 > User.find(3).personal_portfolios.first.portfolios_as_viewer.count
User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1
PersonalPortfolio Load (0.5ms) SELECT `portfolios`.* FROM `portfolios` INNER JOIN `stakes` ON `portfolios`.`id` = `stakes`.`possession_id` WHERE `stakes`.`type` IN ('UserStakeInPortfolio') AND `portfolios`.`type` IN ('PersonalPortfolio') AND `stakes`.`owner_id` = 3 AND (`portfolios`.`type` = 'PersonalPortfolio') LIMIT 1
(0.7ms) SELECT COUNT(*) FROM `portfolios` INNER JOIN `permissions` ON `portfolios`.`id` = `permissions`.`entity_id` WHERE `permissions`.`type` IN ('PortfolioPermission') AND `permissions`.`owner_id` = 3 AND (can_view_info = true || can_edit_cap_table = true || can_view_key_metric = true || can_view_all_metrics = true || can_view_cap_table = true || can_edit_managers = true || can_edit_info = true || can_edit_metrics = true)
=> 3
1.9.3-p362 :126 > User.find(3).personal_portfolios
User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1
PersonalPortfolio Load (0.5ms) SELECT `portfolios`.* FROM `portfolios` INNER JOIN `stakes` ON `portfolios`.`id` = `stakes`.`possession_id` WHERE `stakes`.`type` IN ('UserStakeInPortfolio') AND `portfolios`.`type` IN ('PersonalPortfolio') AND `stakes`.`owner_id` = 3 AND (`portfolios`.`type` = 'PersonalPortfolio')
=> [#<PersonalPortfolio id: 3, name: "Manager Man's portfolio", established: "2013-09-24", created_at: "2013-09-24 23:20:34", updated_at: "2013-09-24 23:20:34", type: "PersonalPortfolio", base_capital_invested: nil, total_shares: nil, logo_file_name: nil, logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil, city: nil, state: nil, invited_manager_email: nil, claim_code: nil, angellist_url: nil, facebook_url: nil, linkedin_url: nil, twitter_url: nil, website_url: nil, longitude: nil, latitude: nil, is_single_use_fund: false, tagline: nil, thumb_file_name: nil, thumb_content_type: nil, thumb_file_size: nil, thumb_updated_at: nil, use_committed_capital: true, owner_id: 3>]
1.9.3-p362 :127 > User.find(3).personal_portfolios.count
User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1
(0.4ms) SELECT COUNT(*) FROM `portfolios` INNER JOIN `stakes` ON `portfolios`.`id` = `stakes`.`possession_id` WHERE `stakes`.`type` IN ('UserStakeInPortfolio') AND `portfolios`.`type` IN ('PersonalPortfolio') AND `stakes`.`owner_id` = 3 AND (`portfolios`.`type` = 'PersonalPortfolio')
=> 1
另一个变体(为了说明/简单
如果我将定义更改personal_portfolio.rb
为:
has_many :portfolio_permissions_as_viewer, :class_name => 'PortfolioPermission', :foreign_key => :owner_id, :conditions => { :can_view_info => true }
User.find(3).viewable_portfolios == User.find(3).personal_portfolios.first.portfolios_as_viewer
仍然返回假。但User.find(3).viewable_portfolios
SELECT `portfolios`.*
FROM `portfolios`
INNER JOIN `permissions`
ON `portfolios`.`id` = `permissions`.`entity_id`
INNER JOIN `portfolios` `personal_portfolios_viewable_portfolios_join`
ON `permissions`.`owner_id` =
`personal_portfolios_viewable_portfolios_join`.`id`
INNER JOIN `stakes`
ON `personal_portfolios_viewable_portfolios_join`.`id` =
`stakes`.`possession_id`
WHERE `permissions`.`type` IN ( 'PortfolioPermission' )
AND `portfolios`.`type` IN ( 'PersonalPortfolio' )
AND `stakes`.`type` IN ( 'UserStakeInPortfolio' )
AND `stakes`.`owner_id` = 3
AND ( `permissions`.`can_view_info` = 1 )
AND ( `personal_portfolios_viewable_portfolios_join`.`type` =
'PersonalPortfolio' )
我认为问题在于它既需要又需要portfolios
,personal_portfolios_viewable_portfolios_join
实际上PersonalPortfolio
应该portfolios
是任何类型的。这让我相信这是我建立关系的方式,但不是 100% 肯定。
需要更多? 如果您需要更多信息/代码,请告诉我。我尽量不要太多余,但可能没有提供足够的帮助。