Forum: Ruby on Rails acts_as_versioned and getting authors

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9904f8acaf66a3c1a4b4434676be11cf?d=identicon&s=25 John Kopanas (Guest)
on 2006-02-14 01:48
(Received via mailing list)
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 Kopanas
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
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-02-14 02:05
(Received via mailing list)
On Feb 13, 2006, at 4:48 PM, John Kopanas 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 Mornini
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Joshua Schairbaum (Guest)
on 2006-02-14 02:05
(Received via mailing list)
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.
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-02-14 02:06
(Received via mailing list)
On 2/13/06, John Kopanas <john@protoseinc.com> wrote:
>
>      Note.create_versioned_table
>
> Thanks for your help :-).  This has been bugging me for a long time.
>
> John Kopanas
> 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...

--
Rick Olson
http://techno-weenie.net
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-02-14 02:18
(Received via mailing list)
On 14 Feb 2006 01:05:16 -0000, Joshua Schairbaum
<devlists-rubyonrails@devlists.com> 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 Olson
http://techno-weenie.net
9904f8acaf66a3c1a4b4434676be11cf?d=identicon&s=25 John Kopanas (Guest)
on 2006-02-14 02:19
(Received via mailing list)
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? :-)

On 13-Feb-06, at 8:06 PM, Rick Olson 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...
> acts_as_versioned.rb
>
> --
> Rick Olson
> http://techno-weenie.net
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails


John Kopanas
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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-02-14 02:51
(Received via mailing list)
On 2/13/06, John Kopanas <john@protoseinc.com> 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? :-)

Well, my apps get it if they're not on edge rails :)

--
Rick Olson
http://techno-weenie.net
Aeac27d9f81191536d3c86ee825d71b2?d=identicon&s=25 Don Mc (dmcclean)
on 2006-02-21 21:44
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 Olson wrote:
> On 14 Feb 2006 01:05:16 -0000, Joshua Schairbaum
> <devlists-rubyonrails@devlists.com> 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 Olson
> http://techno-weenie.net
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-02-21 22:51
(Received via mailing list)
On 2/21/06, don mc <don.mcclean@gmail.com> 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 Olson
http://techno-weenie.net
Bc2672ea3399850eb21af43585e75154?d=identicon&s=25 Gwen Campbell (Guest)
on 2006-11-30 21:33
Rick Olson 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!
Bc2672ea3399850eb21af43585e75154?d=identicon&s=25 Gwen Campbell (Guest)
on 2006-11-30 21:54
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 :)

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. :)
This topic is locked and can not be replied to.