Can you mixin 'has_many', etc., into models?

Folks,

I’m developing a blog plugin which adds a bunch of ‘has_many’
associations to the user object, e.g.:

has_many :blogs_created, :class_name => ‘Blog’, :foreign_key =>
‘created_by’

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!

~Mike

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

On Dec 27, 2007 2:37 PM, Mike L.
[email protected]
wrote:

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!

~Mike

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

Yes, it is possible. Check out how to develop plugins screencasts at
www. Rubyplus.org

Sent from my iPhone

On Dec 26, 2007, at 8:07 PM, Mike L.
<[email protected]

Here is some sample code from a plugin that I developed:
http://sra.rubyforge.org/svn/plugins/simply_rich_association/lib/simply_rich_association.rb

On Dec 26, 2007 8:21 PM, Ryan B. [email protected] wrote:

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


http://www.rubyplus.org/
Free Ruby and Rails Screencasts

On Dec 26, 2007, at 10:21 PM, Ryan B. wrote:

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… :slight_smile:

Peace,
Phillip

On Dec 27, 2007 10:56 PM, Phillip K. [email protected]
wrote:

end
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.

Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

On Dec 26, 2007 11:07 PM, Mike L.
[email protected] wrote:

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


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Dec 27, 2007, at 5:49 PM, Ryan B. wrote:

Define this in your plugin directory:

Hmm. Maybe I should go watch that plugin development screencast… :slight_smile:

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

Peace,
Phillip

Rick Denatale wrote:

On Dec 26, 2007 11:07 PM, Mike L.
[email protected] wrote:

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


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

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