The caveat is that I’m trying to piggy-back off of an existing User
model. If possible, I’d like to have a module with the ‘has_many’
commands, etc. in my plugin/lib folder, and then include said module
into the existing User model. I’ve been able to include other methods
this way, but get the following error message when I try to mixin
‘has_many’:
undefined method `has_many’ for BlogUser:Module
Right now the code looks like this:
class User < ActiveRecord::Base
include BlogUser # Include plugin’s predefined has_many associations
[already existing stuff]
…
(in plugin folder/lib, accessible thanks to Engines)
module BlogUser
has_many :blogs_created, :class_name => ‘Blog’, :foreign_key =>
‘created_by’
…
If you can help, I’d greatly appreciate it! Thanks!
model. If possible, I’d like to have a module with the ‘has_many’
include BlogUser # Include plugin’s predefined has_many associations
If you can help, I’d greatly appreciate it! Thanks!
Couldn’t you just extend the User class in your plugin?
Define this in your plugin directory:
class User < ActiveRecord::Base
has_many :blogs
end
I’ve not done any plugin development, but wouldn’t that require that
the application using the plugin have a User class? It seems more
like you’d want to introspectively add the has_many to the “parent”
thing of BlogUser.
Hmm. Maybe I should go watch that plugin development screencast…
Surprisingly not. If there is not a User class it’ll create a User class
for
you. If there is a User class, it’ll just extend it. That’s the
brilliance
of Ruby.
model. If possible, I’d like to have a module with the ‘has_many’
include BlogUser # Include plugin’s predefined has_many associations
[already existing stuff]
…
(in plugin folder/lib, accessible thanks to Engines)
module BlogUser
has_many :blogs_created, :class_name => ‘Blog’, :foreign_key =>
‘created_by’
…
Has_many is a class method, you need to invoke it against the class
which includes your module:
module BlogUser
def self.included(includer)
includer.class_eval do
has_many :blogs_created, :class_name => ‘Blog’,
:foreign_key => ‘created_by’
end
end
end
Hmm. Maybe I should go watch that plugin development screencast…
Peace,
Phillip
Surprisingly not. If there is not a User class it’ll create a User
class for you. If there is a User class, it’ll just extend it.
That’s the brilliance of Ruby.
Hi Ryan,
I seem to have overlooked
Define this in your plugin directory:
I see what you’re getting at now. By putting this in the plugin, it
will either extend the User class or create it (to basically repeat
what you just wrote). Nice point. +1
model. If possible, I’d like to have a module with the ‘has_many’
include BlogUser # Include plugin’s predefined has_many associations
[already existing stuff]
…
(in plugin folder/lib, accessible thanks to Engines)
module BlogUser
has_many :blogs_created, :class_name => ‘Blog’, :foreign_key =>
‘created_by’
…
Has_many is a class method, you need to invoke it against the class
which includes your module:
module BlogUser
def self.included(includer)
includer.class_eval do
has_many :blogs_created, :class_name => ‘Blog’,
:foreign_key => ‘created_by’
end
end
end
Very interesting. This would be most useful for plugins, because you
could have the user define any already existing class to receive the
class methods.
Thanks to everyone for your suggestions!
Mike
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.