Reputation: 6579
I'm new to Rails and Ruby. I'm trying to implement Data-Context-Interaction /aka DCI/ paradigm in Rails 3.2.11 app. I have User
model which one-to-many
association with Topic
model. I'm trying to write a mixin to User
, but they're not working, please can you offer some debugging help.
My mixing looks like:
module Speaker
extend ActiveSupport::Concern
included do
has_many :assigned_topics, class_name: 'Topic', foreign_key: 'speaker_id'
end
def add_topic(topic)
topic.speaker = self
topic.save
end
def remove_topic(topic)
topic.speaker = nil
topic.save
end
end
When I run below code I get an error:
u = User.first
u.extend Speaker
u.assigned_topics
NoMethodError: undefined method `assigned_topics' for #<User:0x00000002f5dca8>
Upvotes: 1
Views: 549
Reputation: 1086
Do NOT use the DCI pattern in Rails. At least if you expect to get some traffic. Extending an existing object is currently terribly slow. It crashes the caching mechanism of every single ruby implementation.
I know there was some fuzz about this overrated stuff during the last weeks, but really: do not use it this way. It's only a big hassle and much drama around. Rails is not a Java framework, and therefore it hasn't the same problems like Java.
If you want to move stuff from models to modules, use a simple concern, and include it hardcoded in the model(s). No live-extend at runtime and all the hoops. Here is a gist from @dhh how to use it: https://gist.github.com/1014971
Upvotes: 4