I’ve been using polymorphic joins from edge rails based on about 5 lines
of advice Rick O. gave me over IRC. There’s some sparse documentation
popping up but it seems to cover a somewhat different case.
Anyway - I pretty much got it to work - save for one detail. Here’s what
class Event < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
#events table has attachable_type and attachable_id fields
class Task < ActiveRecord::Base
has_many :events, :as => :attachable
#tasks tables is nothing special
and now a unit test:
#there’s some fixtures above
assert tasks(:simple_task).events << events(:simple_event_1)
assert tasks(:simple_task).events << events(:simple_event_2)
assert_equal 2, tasks(:simple_task).events.size
assert_equal “simple event 1”,
assert_equal “simple task”, events(:simple_event_1).task.title
All asserts pass except the last one:
Loaded suite test/unit/task_test
Finished in 1.103896 seconds.
NoMethodError: undefined method
task' for #<Event:0x22258a0> _RAILS_ROOT_/config/../vendor/rails/activerecord/lib/active_record/base.rb:1565:inmethod_missing’
2 tests, 7 assertions, 0 failures, 1 errors
(path replaced with RAILS_ROOT since I’m secretive like that)
In other words - I can do stuff like @task.events << @event but NOT
@event.task = @task ! (or indeed @event.task.* ) @tasks.events <<
definitely works - I even checked the db and the attachment_type/id
fields are set exactly as expected.
It kind of makes sense - since I never actually mention :tasks anywhere
in Event’s model definition. But how exactly do I mention it?
I looked over the example in the wiki but it doesn’t seem to touch on
this problem at all (and it kind of tries to do something different too)
- SO - any help is apretiated.
It could also be that this particular piece of functionality simply
hasn’t been implemented yet…