On 7 July 2010 21:59, Robert Walker <li...@ruby-forum.com> wrote:
> RubyonRails_newbie wrote:
>>  def comment
>>     �...@blogcomment = Blogcomment.find(:all, :order => "created_at
>> desc")
>
> I don't understand the point to this. See next comment...
>
>>       @user = User.find(session[:user_id])
>
> Nor this...
>
>>
>>      Blogpost.find(params[:id]).blogcomments.create(params[:comment])
>>         flash[:notice] = "Added your comment"
>>         redirect_to :action => "show", :id => params[:id]
>
> Here you are unconditionally redirecting to :action => "show" so what's
> the point of the above fetch of Blogcomment?
>
>>   end
>
> By the way, this method is trying to do too much.
>
> - It's fetching an array of Blogcomments (that it doesn't seem to do
> anything with)
> - It finds a user (and doesn't seem to use it either)
> - It finds a Blogpost and creates a new Blogcomment on it.
> - It sets flash[:notice]
> - And finally redirects to the "show" action
>
> The only effective thing it does should be handled by the Blogcomment's
> "create" method anyway.
> At least in the modern RESTful Rails world. You have written a custom
> method for something that can be easily handled by the basic CRUD
> actions of RESTful controllers.
>
> The redirect_to will create a new request/response cycle causing all
> instance variables set in this method to be freed for garbage
> collection.

Robert is correct of course, I had missed the redirect_to.  A case of
woods and trees I think.

OP - if what you are trying to to is _always_ sort the comments in
that order then consider providing a default_scope on the comments
model.  Then they will always be so sorted without any extra work on
your part, even when you access them as blogpost.comments for example.

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to