Acts_as_versioned and getting authors


#1

Hey guys and gals,

I have the following object that has acts_as_versioned:

class Note < ActiveRecord::Base
acts_as_versioned
belongs_to :user
end

The schema for my notes table is as follows:

 create_table :notes, :force => true do |t|
   t.column :id, :integer
   t.column :noteshare_id, :integer
   t.column :user_id, :integer
   t.column :title, :string
   t.column :content, :text
   t.column :created_at, :datetime
   t.column :updated_at, :datetime
   t.column :version, :integer
 end
 Note.create_versioned_table

Now I want to be able to do:

	<% @notes.versions.each do |version| %>
		<%= version.user.fullname %><br/>
	<% end %>

But I can’t!

User is not associated with versions. How can I associate it?

Thanks for your help :-). This has been bugging me for a long time.

John K.
http://www.kopanas.com

=====================================================================
http://www.soen.info - source of the freshest software engineering
information on the net
http://cusec.soen.info - software engineering conference


#2

On Feb 13, 2006, at 4:48 PM, John K. wrote:

Now I want to be able to do:

  <% @notes.versions.each do |version| %>
  	<%= version.user.fullname %><br/>
  <% end %>

But I can’t!

User is not associated with versions. How can I associate it?

User.find(version.user_id).fullname


– Tom M.


#3

I think what you’ll need to do is create a DocumentVersion model and
adds a belongs_to to that. I think it would work then.


#4

On 14 Feb 2006 01:05:16 -0000, Joshua S.
removed_email_address@domain.invalid wrote:

I think what you’ll need to do is create a DocumentVersion model and
adds a belongs_to to that. I think it would work then.

If you want to modify the versioned class, try this:

class Note < ActiveRecord::Base
belongs_to :user
end

Note.versioned_class.class_eval do
belongs_to :user
end

This will work on older versions of acts_as_versioned too.


Rick O.
http://techno-weenie.net


#5

On 2/13/06, John K. removed_email_address@domain.invalid wrote:

 Note.create_versioned_table

Thanks for your help :-). This has been bugging me for a long time.

John K.
http://www.kopanas.com

Try this:

class Note < ActiveRecord::Base
acts_as_versioned do
belongs_to :user
end
end

This is a fairly new feature that I’ve added. I haven’t bothered to
update the gem or rdocs though, plugins are more agile. And I don’t
have that stuff all set up on my powerbook yet. I’m quite sure this
will require edge rails too.

Note: this creates an extension module that will be applied to both
Note and Note::Version. No need to call belongs_to on the model then.

http://techno-weenie.net/svn/projects/plugins/acts_as_versioned/lib/acts_as_versioned.rb


Rick O.
http://techno-weenie.net


#6

That is great Rick. I can’t test it though because my application
for some odd reason does not like edge rails. I am always getting
unitialized constant. Any insight into that? :slight_smile:

On 13-Feb-06, at 8:06 PM, Rick O. wrote:

The schema for my notes table is as follows:
end
User is not associated with versions. How can I associate it?
belongs_to :user

http://techno-weenie.net/svn/projects/plugins/acts_as_versioned/lib/
acts_as_versioned.rb


Rick O.
http://techno-weenie.net


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

John K.
http://www.kopanas.com

=====================================================================
http://www.soen.info - source of the freshest software engineering
information on the net
http://cusec.soen.info - software engineering conference


#7

On 2/13/06, John K. removed_email_address@domain.invalid wrote:

That is great Rick. I can’t test it though because my application
for some odd reason does not like edge rails. I am always getting
unitialized constant. Any insight into that? :slight_smile:

Well, my apps get it if they’re not on edge rails :slight_smile:


Rick O.
http://techno-weenie.net


#8

On 2/21/06, don mc removed_email_address@domain.invalid wrote:

This will work for me. My natural followup questions to this is
how can two versioned classes be linked together? Assuming Parent
has_many Children and a Child belongs_to Parent, I would want
Parent_version to have_many Children_Version and a Child_Version
to belong_to Parent_version. Is this easily doable ?

Regards,
Don

Sure, just reopen the class at the bottom of the file.

class Foo < AR::Base
acts_as_versioned
end

Foo.versioned_class.class_eval do

extra stuff

end


Rick O.
http://techno-weenie.net


#9

This will work for me. My natural followup questions to this is
how can two versioned classes be linked together? Assuming Parent
has_many Children and a Child belongs_to Parent, I would want
Parent_version to have_many Children_Version and a Child_Version
to belong_to Parent_version. Is this easily doable ?

Regards,
Don

Rick O. wrote:

On 14 Feb 2006 01:05:16 -0000, Joshua S.
removed_email_address@domain.invalid wrote:

I think what you’ll need to do is create a DocumentVersion model and
adds a belongs_to to that. I think it would work then.

If you want to modify the versioned class, try this:

class Note < ActiveRecord::Base
belongs_to :user
end

Note.versioned_class.class_eval do
belongs_to :user
end

This will work on older versions of acts_as_versioned too.


Rick O.
http://techno-weenie.net


#10

Gwen Campbell wrote:

I think I must be misunderstanding something, or doing something wrong.

Murphy must have a law, yes, stating that one will only realize the
solution after publicly asking for help :slight_smile:

The issue, of course, was that I was not specifying the foreign_key in
the versioned table.

Thing.versioned_class.class_eval do
belongs_to :created_by, :class_name => “User”, :foreign_key =>
“created_by”
belongs_to :updated_by, :class_name => “User”, :foreign_key =>
“updated_by”
end

is what was needed.

I’m still not clear why

class Thing < ActiveRecord::Base
acts_as_versioned do
belongs_to :created_by, :class_name => “User”, :foreign_key =>
“created_by”
belongs_to :updated_by, :class_name => “User”, :foreign_key =>
“updated_by”
end
end

doesn’t do the same, but that’s academic at this point. :slight_smile:


#11

Rick O. wrote:

Try this:

class Thing < ActiveRecord::Base
acts_as_versioned do
belongs_to :user
end
end

I think I must be misunderstanding something, or doing something wrong.
When I modified my acts_as_versioned class (in my case “Thing”) as
described above, I get a NoMethodError: undefined method ‘belongs_to’
for #Module:0x2791c00.

Alternatively, I tried

class Thing < ActiveRecord::Base
belongs_to :user
end

Thing.versioned_class.class_eval do
belongs_to :user
end

and while there are no errors, eager-loading doesn’t happen when I’m
viewing one of my versions.

I’m using EdgeRails, and version 0.5.1 (Oct 5, 2006) of the AAV plugin.
If it makes a difference, my user model is provided by
ActsAsAuthenticated, and the related fields in my Thing model
(created_by, and updated_by) are being handled dynamically by the
userstamp plugin ( http://delynnberry.com/projects/userstamp/ ).

If there are any pertinent details that I’m leaving out, please let me
know! Any pointers anyone could offer would be very much appreciated!