2 questions... Can anyone help?

hello there,

I’ve just signed up to this, so apologies for the 2 questions in one
post…

please can anyone help with the following 2 issues I have? and what I
need to do to fix them?
Note - I’m quite new to rails, so if anyone can explain these, i’d be
grateful…

Question 1:

The search functionality on my application appears to have stopped
working. When I type a name in the field and hit search, I get:

''NoMethodError in BuddiesController#search
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.info ‘’

The search method is below. The line in red, is where the stack trace
led me to, but I haven’t altered anything, so unsure why it aint working
now.

def search

  @title = "Find Buddies"

  if params[:q]
    query = params[:q]
    curr_page = params[:page] || 1
    users = User.find_with_ferret(query)
     infos = Info.find_with_ferret(query)
     hits = infos
     users.concat(hits.collect { |hit| hit.user }).uniq!
     users.each  { |user| user.info ||= Info.new }
     users = users.sort_by { |user| user.info.last_name }
     @buddies = users.paginate(:page => curr_page, :per_page => 10)

   end

end

Question 2:

Within my application, a user can post a comment, such as a status
update… Similar to that on Facebook and Twitter. However, whoever I
log in as, I see the same posts. I know this is probably to do with
logged_in? or sessions, but not sure how to amend it.

Can anyone help?

Many many Thanks!

the note in red (which obviously you cant see here) is this line:

users.each { |user| user.info ||= Info.new }

Hope you can help…

:slight_smile:

Craig W. wrote:

the note in red (which obviously you cant see here) is this line:

users.each { |user| user.info ||= Info.new }

Hope you can help…

:slight_smile:

Just like it says. Some object on which you’re trying to call a method
is nil instead of a User.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Craig W. wrote:

the note in red (which obviously you cant see here) is this line:

users.each { |user| user.info ||= Info.new }

Hope you can help…

:slight_smile:

Just like it says. Some object on which you’re trying to call a method
is nil instead of a User.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Thanks for the quick reply.
Looking at the code, do you know what needs changing? I’m not 100% sure,
so any advice would be great.

I am slowly learnign rails, but when an issue like this arises, I find
it hard to knoww hat exactly needs amending to allow the application to
render correctly…

Thanks again,

Thanks for the reply - some useful ideas for me to look at!

Cheers

Craig W. wrote:

Question 1:

def search

  @title = "Find Buddies"

  if params[:q]
    query = params[:q]
    curr_page = params[:page] || 1
    users = User.find_with_ferret(query)
     infos = Info.find_with_ferret(query)
     hits = infos
     users.concat(hits.collect { |hit| hit.user }).uniq!
     users.each  { |user| user.info ||= Info.new }
     users = users.sort_by { |user| user.info.last_name }
     @buddies = users.paginate(:page => curr_page, :per_page => 10)

   end

end

Have you considered what would happen when "if params[:q] == false?

Here is my #1 suggestion for you: do some research on writing automated
tests for your code. Any decent set of controller tests (or specs) would
show you where and why this is failing. What Rails is telling you is
that you are trying to send a message to a nil object (mostly likely
users). Ruby does not allow sending messages to nil objects (Note: some
languages do allow messages to nil, which can be very tricky to debug
without proper tests/specs).

A simple controller expectation could be used to ensure that the
variables, which should be assigned in the controller, actually do get
assigned.

In rSpec, for example you could use assigns(:users) in your search
action spec. If later you find a case where that doesn’t happen you’ll
know immediately because the spec would start failing.

Question 2:

Within my application, a user can post a comment, such as a status
update… Similar to that on Facebook and Twitter. However, whoever I
log in as, I see the same posts. I know this is probably to do with
logged_in? or sessions, but not sure how to amend it.

It sounds to me like you’re not scoping your comments properly. Let’s
take the Facebook “wall” as an example: A user’s wall is scoped under
that user. Other users can post the any of their friend’s walls. When
you view a wall the entries are scoped under that user.

There are a number of ways to implement this scoping. One way is through
filtering (a condition on the query). Another way, and what I would use
in this situation, is to scope based on ActiveRecord associations.

Example:
@comments = current_user.comments
or
@user = current_user.friends.find_by_login(“johndoe”)
@comments = @user.comment

Where “current_user” is a method to get the signed in user from the
authentication system.

Notice how everything is “scoped” @comments can only ever contain
comments related to a specific user. And, @user can only ever be
assigned to one of the signed in user’s friends.

Craig W. wrote:

users = users.sort_by { |user| user.info.last_name }
Would I need to change |user| to User? Is this where the issue is?

Watch this little irb session:

$ irb

arr = []
=> []

arr << “hello”
=> [“hello”]

arr << “world”
=> [“hello”, “world”]

arr << nil
=> [“hello”, “world”, nil]

users = users.sort_by { |user| user.info.last_name }

''NoMethodError in BuddiesController#search
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.info ‘’

Now look back at your error. Does it make sense to you now?

This is why it is so vital important to test your code. Never expect
code to “just work.” Be diligent about considering all possible failure
modes and write tests for them. If you miss one, immediately write a
test for it to make sure it never happens a second time.

Robert W. wrote:

Craig W. wrote:

users = users.sort_by { |user| user.info.last_name }
Would I need to change |user| to User? Is this where the issue is?

Watch this little irb session:

$ irb

arr = []
=> []

arr << “hello”
=> [“hello”]

arr << “world”
=> [“hello”, “world”]

arr << nil
=> [“hello”, “world”, nil]

users = users.sort_by { |user| user.info.last_name }

''NoMethodError in BuddiesController#search
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.info ‘’

Now look back at your error. Does it make sense to you now?

This is why it is so vital important to test your code. Never expect
code to “just work.” Be diligent about considering all possible failure
modes and write tests for them. If you miss one, immediately write a
test for it to make sure it never happens a second time.

what you’ve done makes sense, but i still don’t see the error in my
code…

i’ll have to do some more digging i think.

cheers…

so… looking at my code:

def search

  @title = "Find Buddies"

  if params[:q]
    query = params[:q]
    curr_page = params[:page] || 1
    users = User.find_with_ferret(query)
     infos = Info.find_with_ferret(query)
     hits = infos
     users.concat(hits.collect { |hit| hit.user }).uniq!
     users.each  { |user| user.info ||= Info.new }
     users = users.sort_by { |user| user.info.last_name }
     @buddies = users.paginate(:page => curr_page, :per_page => 10)

   end

end

using this snippet of code:

users = users.sort_by { |user| user.info.last_name }
Would I need to change |user| to User? Is this where the issue is?

Craig W. wrote:

Robert W. wrote:

Craig W. wrote:

users = users.sort_by { |user| user.info.last_name }
Would I need to change |user| to User? Is this where the issue is?

Watch this little irb session:

$ irb

arr = []
=> []

arr << “hello”
=> [“hello”]

arr << “world”
=> [“hello”, “world”]

arr << nil
=> [“hello”, “world”, nil]

users = users.sort_by { |user| user.info.last_name }

''NoMethodError in BuddiesController#search
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.info ‘’

Now look back at your error. Does it make sense to you now?

This is why it is so vital important to test your code. Never expect
code to “just work.” Be diligent about considering all possible failure
modes and write tests for them. If you miss one, immediately write a
test for it to make sure it never happens a second time.

what you’ve done makes sense, but i still don’t see the error in my
code…

i’ll have to do some more digging i think.

cheers…

update

It seems that if I type the word test into the search field and click
search, a result is returned,

If i attempt that again using a surname, or any other search, I get the
error…

How strange. Does anyone have an explanation for this?

thanks again,