Forum: Ruby on Rails acts_as_commentable release

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.
8c7a98649cdbbdf39eeed17ca9bdbbd3?d=identicon&s=25 Josh Charles (Guest)
on 2006-05-15 22:05
(Received via mailing list)
I now have the acts_as_commentable plugin up on RubyForge.  This
plugin will allow you to add comments to any active_record object in
your Rails application.

So far the directions are simple, and there are only a few features:

To install:
ruby script/plugin install
svn://rubyforge.org//var/svn/commentable/acts_as_commentable

In the readme there is a sample migration you will need to use, with
directions on how to modify it if need be.  I'm still learning how the
plugins work, and do not know if there is a way to autogenerate this.

Once the migration is completed and ran, you can just add:
acts_as_commentable to those objects you want to comment on and you're
all set.

Lets say you have a Person object:

class Person < ActiveRecord::Base
  acts_as_commentable
end

Then you have a person:

p = Person.find(:first)

Create a comment:

c = Comment.new

Fill out the comment

c.name = "Name of Commentor"
c.content = "What they wrote"

then add it to your object:

p.add_comment(c)

to remove a comment:

p.remove_comment(c)

I think that's everything.  This was done quick and dirty, If there
are things you would like to see in this, let me know.  I don't know
if there is much more to do that wouldn't be application specific.

Thanks
Josh
2ddece96c2f2046f723ee37485e86be5?d=identicon&s=25 Benjamin Curtis (Guest)
on 2006-05-15 23:11
(Received via mailing list)
Published at http://agilewebdevelopment.com/plugins/
acts_as_commentable :)

--
Benjamin Curtis
http://www.tesly.com/ -- Collaborative test case management
http://www.agilewebdevelopment.com/ -- Resources for the Rails community
E4b8c35c29cfcaea3c2101193417466b?d=identicon&s=25 Gene Kahn (kublai)
on 2006-07-06 21:47
Josh,
Thank you for this wonderful plugin. Exactly what I need. Unfortunately,
as a noobie, I don't know how to put the parts together.

First, should there be a comment_controller and a comment.rb model?

>
> Lets say you have a Person object:
>
> class Person < ActiveRecord::Base
>   acts_as_commentable
> end
>

Ok, I've done that.

> Then you have a person:
>
> p = Person.find(:first)
>
> Create a comment:
>
> c = Comment.new
>
I'd like to put the capability to add a comment to a person instance
during Create, Edit, and Show of a person instance. Where do I put this
command: c = Comment.new? Which controller? Which action?

>
> Fill out the comment
>
> c.name = "Name of Commentor"
> c.content = "What they wrote"
>

This filling out of the comment should happen in a New.rhtml comment
view, right?

> then add it to your object:
>
> p.add_comment(c)
>
> to remove a comment:
>
> p.remove_comment(c)
>

The above must be done in a Create and Destory action in the
comment_controller, right?

I apologize for these jumbo dumbo questions. I'm just beginning to get a
feel for the broad strokes; the massive details still elude me.

Thanks,
gk
E4b8c35c29cfcaea3c2101193417466b?d=identicon&s=25 Gene Kahn (kublai)
on 2006-07-06 22:05
Hi,

The data table supporting acts_as_commentable has the following
definition:

  def self.up
    create_table "comments", :force => true do |t|
      t.column "title", :string, :limit => 50, :default => ""
      t.column "comment", :string, :default => ""
      t.column "created_at", :datetime, :null => false
      t.column "commentable_id", :integer, :default => 0, :null => false
      t.column "commentable_type", :string, :limit => 15, :default =>
"", :null => false
      t.column "user_id", :integer, :default => 0, :null => false
    end

What do the columns "commentable_id" and "commentable_type" function as?
Would commentable_id be the id of the instance of the object being
commented on, and commentable_type be the name of its class?

Thanks again,
gk
96931bfe0c2948f47a98e15ae52e5637?d=identicon&s=25 Chris Carter (cdcarter)
on 2006-07-07 06:03
Hi Gene,
It uses a polymorphic association.  The commentable_id and type work as
you thoguht tahy did.  You can find mor about polymorphic associations
in rails on the wiki.

Chris.

Gene Kahn wrote:
> Hi,
>
> The data table supporting acts_as_commentable has the following
> definition:
>
>   def self.up
>     create_table "comments", :force => true do |t|
>       t.column "title", :string, :limit => 50, :default => ""
>       t.column "comment", :string, :default => ""
>       t.column "created_at", :datetime, :null => false
>       t.column "commentable_id", :integer, :default => 0, :null => false
>       t.column "commentable_type", :string, :limit => 15, :default =>
> "", :null => false
>       t.column "user_id", :integer, :default => 0, :null => false
>     end
>
> What do the columns "commentable_id" and "commentable_type" function as?
> Would commentable_id be the id of the instance of the object being
> commented on, and commentable_type be the name of its class?
>
> Thanks again,
> gk
E4b8c35c29cfcaea3c2101193417466b?d=identicon&s=25 Gene Kahn (kublai)
on 2006-07-07 17:29
Chris Carter wrote:
> Hi Gene,
> It uses a polymorphic association.  The commentable_id and type work as
> you thoguht tahy did.  You can find mor about polymorphic associations
> in rails on the wiki.
>
> Chris.
>
> Gene Kahn wrote:
>> Hi,
>>
>> The data table supporting acts_as_commentable has the following
>> definition:
>>
>>   def self.up
>>     create_table "comments", :force => true do |t|
>>       t.column "title", :string, :limit => 50, :default => ""
>>       t.column "comment", :string, :default => ""
>>       t.column "created_at", :datetime, :null => false
>>       t.column "commentable_id", :integer, :default => 0, :null => false
>>       t.column "commentable_type", :string, :limit => 15, :default =>
>> "", :null => false
>>       t.column "user_id", :integer, :default => 0, :null => false
>>     end
>>
>> What do the columns "commentable_id" and "commentable_type" function as?
>> Would commentable_id be the id of the instance of the object being
>> commented on, and commentable_type be the name of its class?
>>
>> Thanks again,
>> gk

Hi, Chris,
So then one can pull out easily from a single table comments about an
instance, about all instances of a class, by a user, or list comments in
descending date  ("Recent Comments") order. Great! Just what I want.
Thanks for the tip on poly; I'll pursue.
gk
Fca4e547bac6164b57b8a7bb38f4e6a0?d=identicon&s=25 koloa (Guest)
on 2006-12-08 14:19
hello,
if i have a post that acts as commentable and i need to find all
comments relating to the post, do i just add the post id to the comments
table, and just do a comments.find :all, :condition where post.id ==
post_id ??
Be90bd7201648971dd21fa7598fec1dd?d=identicon&s=25 jake (Guest)
on 2006-12-08 16:01
koloa wrote:
> hello,
> if i have a post that acts as commentable and i need to find all
> comments relating to the post, do i just add the post id to the comments
> table, and just do a comments.find :all, :condition where post.id ==
> post_id ??


the easiest would be to do:
@post = Post.find(params[:id])
@comments = @post.comments


That will give you only comments for that post.


(unless the associations are different)

--jake
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 01:06
hello i have a question,


in the directions at
http://www.juixe.com/techknow/index.php/2006/06/18.......
it states that there must be a user model? since i am applying
acts_as_commentable to a recipe, do i change the user_id in the comments
table to a recipe_id?

also, i am receiving this error

Mysql::Error: #42S02Table 'howtodb.commentings' doesn't exist: SHOW
FIELDS FROM commentings

my database name is howtodb, my application name is called howto, and i
have a table called howto which is the model that is acting like a
comments.

thanks for any help!
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 01:08
also i notice this..

in the webpage, the migration table is this

def self.up
  create_table :comments, :force => true do |t|
    t.column :title, :string, :limit => 50, :default => ""
    t.column :comment, :string, :default => ""
    t.column :created_at, :datetime, :null => false
    t.column :commentable_id, :integer, :default => 0, :null => false
    t.column :commentable_type, :string, :limit => 15,
      :default => "", :null => false
    t.column :user_id, :integer, :default => 0, :null => false
  end

  add_index :comments, ["user_id"], :name => "fk_comments_user"
end

def self.down
  drop_table :comments
end

in the readmen file the migration table is this:
 def self.up
    create_table :commentings do |t|
      t.column :commentable_id, :integer
      t.column :comment_id, :integer
      t.column :commentable_type, :string
    end
    create_table :comments do |t|
      t.column :name, :string
      t.column :content, :text
    end
  end

  def self.down
    drop_table :commentings
    drop_table :comments
  end


there is suppose to be a commenting table?
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 01:57
hello, everything seems to be working now after i generated the
commenting table.

did i do this correctly since the webpage i read didnt have anything for
the commentings table in the migration?
3e6bbd5a01437feec48adf3838b15d91?d=identicon&s=25 askegg (Guest)
on 2006-12-14 02:02
(Received via mailing list)
You (the user) are adding comments to the recipe, so
acts_as_commentable expects a table called "user" with an id field.
There is no need to change the tables as the association is polymorphic
(it can apply to any model), hence the commentable_type and
commentable_id fields.

My README file defines a comments table - not sure where you are
getting the commentings from....

How are you calling up the comments to produce that error?
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 02:14
hello askegg! thanks for helping....


here is what the readme says:

  def self.up
    create_table :commentings do |t|
      t.column :commentable_id, :integer
      t.column :comment_id, :integer
      t.column :commentable_type, :string
    end
    create_table :comments do |t|
      t.column :name, :string
      t.column :content, :text
    end
  end

  def self.down
    drop_table :commentings
    drop_table :comments
  end

here is what i originaly only had for acts_as_commentable

  def self.up
      create_table :comments, :force => true do |t|
        t.column :title, :string, :limit => 50, :default => ""
        t.column :comment, :string, :default => ""
        t.column :created_at, :datetime, :null => false
        t.column :commentable_id, :integer, :default => 0, :null =>
false
        t.column :commentable_type, :string, :limit => 15,
          :default => "", :null => false
        t.column :user_id, :integer, :default => 0, :null => false
      end

      add_index :comments, ["user_id"], :name => "fk_comments_user"
    end


..........

i can do this in my controller just fine..
  @asset = Howto.find(1)
  comment = Comment.new(:title => "title!!!", :comment => "some silly
comment,...")
  logger << "COMMENT #{comment.comment}n"
  @asset.add_comment comment

and when i check for the information in the comments and the commentings
table, the information is there.

but when i do this to pass the information to a getDetail view page

   @howto = Howto.getHowto(params[:id])
    @comments = @howto.comments

i get this error.... undefined method `comments' for #<Array:0x22bab08>



and when i do a debug(@howto)


i do not see the relationship between my Howto model and acts as
commentable....

i have a user table from acts as authenticated..could that cause an
issue?

do i need to add a belongs_to :howto in the comment.rb file?


Thanks for any help!!!
3e6bbd5a01437feec48adf3838b15d91?d=identicon&s=25 askegg (Guest)
on 2006-12-14 02:40
(Received via mailing list)
Hmmm, the copy I have does not include any mention of a commentings
table - I think I can safely ignore it.  Perhaps you should make sure
you have the latest version first?

The comments table schema looks exactly the same as mine.  The usage in
the controller looks OK, but what does the getHowTo method in the HowTo
model do?

The relationship between the Howto model should be built by the plugin:

class Howto < ActiveRecord::Base
  acts_as_commentable
end

So long as there is a user account with an id field, comments should be
happy.  This should not really matter unless you are searching for
comments by a particular user.

You should not need to add anything to the comment.rb file as you are
not asking "which of all these comments belongs to this howto?" but
"Given this howto, what are the comments made?".  The difference is in
the direction of the question.

Do me a favor?  First restart your webserver and view the page - it
might work.  Then refresh the page without restarting (it might fail).
Repeat to make sure.  There may be a bug in the plugin (I am
experiencing similar issues).

After that - have a look at your custom getHowto method and get back to
me.
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 02:41
i have reinstalled from original source from above...but when executing
@howto.add_comment(c), i am getting method not found.

do i need to modify the comment.rb file to put the belongs_to howto
class?
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 02:47
a little more info


this is my howto class

class Howto < ActiveRecord::Base

 acts_as_commentable
 acts_as_ferret :fields =>["title", "howto", :main_category]
 acts_as_rateable

 has_many :howtophotos
 belongs_to :scategory
 belongs_to :user

--------

basically i dont really care who the comments belong to since its the
public so i dont think ill ever need to search comments by user name...i
am just looking for a way to say....

get my howto and display all the associated comments and give a form so
other people can attach their comments to the howto article.


when i do this

@howto = Howto.find(id)

and print out the @howto via debug in my view, i dont see any
association to the acts_as_commentable. because of this i guess thats
the reason why @howto.add_comment(c) doesnt work...

hmmmm
3e6bbd5a01437feec48adf3838b15d91?d=identicon&s=25 askegg (Guest)
on 2006-12-14 02:54
(Received via mailing list)
Try it, but you shouldn't need to.
The plugin adds the methods for you.
3e6bbd5a01437feec48adf3838b15d91?d=identicon&s=25 askegg (Guest)
on 2006-12-14 03:00
(Received via mailing list)
poipu wrote:
...
> when i do this
>
> @howto = Howto.find(id)
>
> and print out the @howto via debug in my view, i dont see any
> association to the acts_as_commentable. because of this i guess thats
> the reason why @howto.add_comment(c) doesnt work...

No, you wont because the method find returns a Howto object, which does
not describe the methods available on the object.  If you want to see
these, start a console (./scripts/console) and type:

howto=Howto.find(1)
howto.methods.sort

You should see add_comment listed....
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 03:01

hmmmm something odd or its working properly.....

this works

    @howto = Howto.find(params[:id])
    c = Comment.new
    c.name = "Name of Commentor"
    c.content = "What they wrote"
    @howto.add_comment c


but this doesnt


    @howto = Howto.getHowto(params[:id])
    c = Comment.new
    c.name = "Name of Commentor"
    c.content = "What they wrote"
    @howto.add_comment c

  where getHowto is a condition in my howto model class
 def self.getHowto(id)
   Howto.find(:all, :conditions => ["id = ?", id])
 end


is there a particular reason why using the bottom example does not
recognize add_comment method?
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 03:03
oh i see...thansk so much for taking the time to help and explain!
appreciate it! now time to work on creating that view...

but...is there a way to get a howto object without the comments? like a
.find( :dontinclude comments)
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 03:11
wait....


  controller
   def view
    @howto = Howto.find(2)
    @howto.comments
end

and when i do the debug @howto, i see the comments...

but when i do without the @howto.comments, the comments are not
available in the view. is this right?

controller
   def view
    @howto = Howto.find(2)
end
3e6bbd5a01437feec48adf3838b15d91?d=identicon&s=25 askegg (Guest)
on 2006-12-14 03:20
(Received via mailing list)
Ahh - your self.getHowto method might be returning an array of howTo's.
 Arrays are not extended by acts_as_commentable, so the method is
unknown.

AR will not load the comments unless asked:
eg.  @howto.comments
OR
Howto.find(params[:id], :include =" :comments)

Of course this is different to displaying them or not.  You may want to
use your view to display howtos without comments first, them a use a
link to display a version *with* comments.
3e6bbd5a01437feec48adf3838b15d91?d=identicon&s=25 askegg (Guest)
on 2006-12-14 04:34
(Received via mailing list)
The first loads the comments into the instance variable @comments,
which can be used in your view.  The second does not because .comments
is never called or stored.

If you get fancy you can create a method to spit back the comments only
and use ajax to populate a div to display them, with some fancy
animation of course.  That way you do not need to create two different
views, you do not load comments by default and you can display your
web2.0 prowess with little effort (thanks to rails magic).

See
http://slash7.com/assets/2006/10/8/RJS-Demistified...
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 04:37
hi askegg, thanks for pointing out that article. what you are describing
is exacyly how i plan on implenting acts as commentable.

askegg wrote:
> The first loads the comments into the instance variable @comments,
> which can be used in your view.  The second does not because .comments
> is never called or stored.
>
> If you get fancy you can create a method to spit back the comments only
> and use ajax to populate a div to display them, with some fancy
> animation of course.  That way you do not need to create two different
> views, you do not load comments by default and you can display your
> web2.0 prowess with little effort (thanks to rails magic).
>
> See
> http://slash7.com/assets/2006/10/8/RJS-Demistified...
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 06:36
i really dont think i broke something, but anyone here ever had your
application stop adding comments to the database?

i can manually enter them in the comments table, but not in my rails
application!
3e6bbd5a01437feec48adf3838b15d91?d=identicon&s=25 askegg (Guest)
on 2006-12-14 07:39
(Received via mailing list)
Check the logs for the SQL statements being made....
Be5736b3bc77d526585c1f17b6692520?d=identicon&s=25 poipu (Guest)
on 2006-12-14 15:24
ahhhhhH!

i see my carelessness again!

add_comment vs add_comments!


thanks again askegg.
D9fc58b64150a4fa6dafd9b5ebe49cff?d=identicon&s=25 bennett.23@osu.edu (Guest)
on 2007-05-17 13:53
<a href = " http://supermeganah.com/nah.html ">nah pizdetc</a>
02d0908979b7e0a120b3224e13711fb5?d=identicon&s=25 lauri.grohn@netsonic.fi (Guest)
on 2007-05-18 15:04
<a href = " http://supermeganah.com/nah.html ">nah pizdetc</a>
324411d3e1192e29c3f991bbac0679d9?d=identicon&s=25 nettime@bbs.thing.net (Guest)
on 2007-05-20 01:57
<a href = " http://supermeganah.com/nah.html ">nah pizdetc</a>
949a93cf7f184ba79be6e748cf8b0a55?d=identicon&s=25 nettime@bbs.thing.net (Guest)
on 2007-05-21 04:04
<a href = " http://supermeganah.com/nah.html ">nah pizdetc</a>
3f76cb824f4a09af19f06022c1b491ab?d=identicon&s=25 pronoun@netcom.no (Guest)
on 2007-05-22 09:37
<a href = " http://supermeganah.com/nah.html ">nah pizdetc</a>
3d94835fb45af2395844615ffe643389?d=identicon&s=25 Mustafa Ekim (maul)
on 2007-11-07 23:42
hey,

I installed&run the plugin successfully. it works with one exception
when I say @event.add_comment my_comment_object
it create 1 comment but 2 commentings.
so @event.comments returns the comments twice.

does any one has an idea why :)
This topic is locked and can not be replied to.