【发布时间】:2010-12-18 09:26:54
【问题描述】:
当我为以下内容调用@user.connections 时,为什么连接表会更新?
连接模型
class Connection < ActiveRecord::Base
belongs_to :left_nodeable, :polymorphic => true
belongs_to :right_nodeable, :polymorphic => true
# Statuses:
PENDING = 0
ACCEPTED = 1
named_scope :pending, :conditions => { :connection_status => PENDING }
named_scope :accepted, :conditions => { :connection_status => ACCEPTED }
end
用户模型
class User < ActiveRecord::Base
has_many :left_connections, :as => :left_nodeable, :class_name => 'Connection', :conditions => {:left_nodeable_type => 'User', :right_nodeable_type => 'User'}
has_many :right_connections, :as => :right_nodeable, :class_name => 'Connection', :conditions => {:right_nodeable_type => 'User', :left_nodeable_type => 'User'}
def connections
self.left_connections << self.right_connections
end
end
如果我使用:
def connections
self.left_connections + self.right_connections
end
然后模型工作正常,但我不能使用我的任何 named_scope 方法。
所以我想我的问题归结为......
ActiveRecord 上的“
【问题讨论】:
标签: ruby-on-rails arrays activerecord