Forum: Ruby on Rails find_by_user_id_and_id question

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.
2927af2b9ddd2f3f34b316755b8f4dfb?d=identicon&s=25 comopasta Gr (dgj)
on 2008-10-14 23:03
Hi,

I wondering about the next:

The book Advance Rails states that using

  def show
    @message = Message.find_by_user_id_and_id(current_user.id,
params[:id])
  end

Should return RecordNotFound if the user that tries to view a message is
not the one that created it.

But what I get is:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.sender_name

And that is fine since it gets to the view and the @message used there
is nil.

But the find_by_user_id_and_id was not returning RecordNotFound. Which
is something I could use a common error page for.

I'm I missing something?

Thanks.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-14 23:39
(Received via mailing list)
On 14 Oct 2008, at 22:03, Pod Caster wrote:

>  end
>
> Should return RecordNotFound if the user that tries to view a
> message is
> not the one that created it.

That's just not what find_by ... does. It behaves more like find :first

Fred
This topic is locked and can not be replied to.