开发者

Rails: associate model with two of its kind

im trying to do this:

class Event < ActiveRecord::Base
  belongs_to :previous_event
  has_one :event, :as => :previous_event, :foreign_key => "previous_event_id"
  belongs_to :next_event
  has_one :event, :as => :next_event, :foreign_key => "next_event_id"
end

because i want to enable the user to repeat events and change multiple oncoming events at the same time. what am i doing wrong, or is there another way of doing this? somehow i need to know about the previous and the next event, don't i? when i'm testing this in the consolewith Event.all[1].previous_event, i get the following error:

NameError: uninitialized constant Event::PreviousEvent
    from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:105:in `const_missing'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2199:in `c开发者_如何学编程ompute_type'
    from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2195:in `compute_type'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:156:in `send'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:156:in `klass'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/belongs_to_association.rb:49:in `find_target'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:239:in `load_target'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:112:in `reload'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1250:in `previous_event'
    from (irb):2

what is going wrong here? thanks a ton for your help.


It should work just like this, there is no need for the belongs_to since it circular anyway.

class Event < ActiveRecord::Base
  has_one :next_event, :class_name => "Event", :foreign_key => "previous_event_id"
  has_one :previous_event, :class_name => "Event", :foreign_key => "next_event_id"
end

The problem here though is that you have to set its next and previous events manually. You could just have a next_event and look up the previous one (or vise versa - depending on which is more efficient in your use case)

class Event < ActiveRecord::Base
  has_one :next_event, :class_name => "Event", :foreign_key => "previous_event_id"
  def previous_event
    Event.first(:conditions => ["next_event_id = ?", self.id])
  end
end


Ah, just found the error, this is the right way to do it:

class Event < ActiveRecord::Base
  belongs_to :previous_event,:class_name => "Event"
  has_one :event, :as => :previous_event, :class_name => "Event", :foreign_key => "previous_event_id"
  belongs_to :next_event,:class_name => "Event"
  has_one :event, :as => :next_event, :class_name => "Event", :foreign_key => "next_event_id"
end

found it here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜