Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert fetching :last from post.comme...

Replytotopic

fetching :last from post.comments

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

Greetings,

After some time building my site, i stumble again on some part of my comments. am a bit confused on why, for example this line

post.comments.last.user.name

will succeed in my development server. however, on a production server, i get an error that i don’t have a method of a ‘user’. ok, i got the idea on why. first, i really don’t have a variable/method for that. but on dev server, i can fetch a ‘Post’ with a last ‘Poster’ with no error.

is there any other way, i can fetch a Post, with a Last Poster on it’s side?, like i was doing a simple forums with set of details on the index.

here’s what i have been doing exactly, i have an association of Posts that has_many comments, and a User that has_many comments, a simple association.

on my comment model: def last find(:all) end

for the controller, just some simple find method

my code in my template.

<%= link_to (post.comments.last.user.name, *) %>

is there a way i can simplify this and make this work on a production server?

Thank you again!. have a good day.

 
Img_7624

Authority 50
Posting Rating 99
Sign in to rate this post

I’m not entirely clear on what you want to do, but I suspect your missing method problem is simpler than you think. In your example code:

post.comments.last.user.name

you don’t handle the case of a post with no comments. Calling #last on an empty collection will return nil, which has no #user method. That’s probably the error you’re seeing.

On an unrelated note, your later example for your Comment model:

def last find(:all) # I assume you mean this to be find(:all).last end

is very much not what you want to do. Firstly, Rails provides a #last finder method, so there’s no reason to reimplement it. More importantly, this will fetch far more from the database than necessary. This will return all matching rows and create ActiveRecord objects in memory for each of them, all but one of which will sit around unused until the garbage collector gets around to them. If you have enough processes doing this at once you can easily bring down a server.

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

Greetings,

how come i did not realize that, calling a #last on an empty collection will return a nil, that’s the error i am seeing. i have a post that has no comments.. maybe needs some more study :D and yes, i mean to say
def last find(:all).last
end
i quitted that, and just use the rails #last method :D

thank you very much again Adam, you always saves Me :D you’re the best!!

Replytotopic

Other Recent Topics

Ask a Rails expert : nested application ApplicationController get called intead of children::ApplicationController

Ask a Rails expert : Best way to structure a database for a large/static dataset

Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)

Ask a Rails expert : Above Ground Pool Supplies

Ask a Rails expert : How to get url params in observer or model in Rails 3.1

Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?

Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?

Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )

Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob

Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel