ActiveRecord find ignores extra characters after numeric id. ?

16 views
Skip to first unread message

fugee ohu

unread,
Jan 15, 2020, 2:17:42 PM1/15/20
to Ruby on Rails: Talk
Doesn't work for me in rails 6

  Parameters: {"id"=>"18-test-title"}
 ActionController::ActionControllerError (Cannot redirect to nil!):

Ne

Ariel Juodziukynas

unread,
Jan 15, 2020, 3:07:00 PM1/15/20
to rubyonra...@googlegroups.com

Person.find("31-sarah") # returns the object for ID = 31
In your case, if you are using find, it should search for record with id = 18

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/64e66550-c393-43cd-8156-29c43139899a%40googlegroups.com.

fugee ohu

unread,
Jan 15, 2020, 4:00:22 PM1/15/20
to Ruby on Rails: Talk


On Wednesday, January 15, 2020 at 3:07:00 PM UTC-5, Ariel Juodziukynas wrote:

Person.find("31-sarah") # returns the object for ID = 31
In your case, if you are using find, it should search for record with id = 18

El mié., 15 ene. 2020 a las 16:18, fugee ohu (<fuge...@gmail.com>) escribió:
Doesn't work for me in rails 6

  Parameters: {"id"=>"18-test-title"}
 ActionController::ActionControllerError (Cannot redirect to nil!):

Ne

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonra...@googlegroups.com.

I've over-riding ActiveRecord to_param in my model

  def to_param
   "#{id}-#{title.parameterize}"
  end

Ariel Juodziukynas

unread,
Jan 15, 2020, 8:26:45 PM1/15/20
to rubyonra...@googlegroups.com
find method doesn't care about the to_param method, it just takes the parameter you use, I guess it calls "to_i" and uses that integer to query the id column

Why don't you show your code, the stacktrace, the logs or anything? I suggest you read something like stackoverflow's guidelines on how to ask, you posts are usually really hard to understand and a lot of information is missing

To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1ba590e7-4c1c-4fcd-9ed8-0f2ed65b0350%40googlegroups.com.

fugee ohu

unread,
Jan 15, 2020, 8:37:22 PM1/15/20
to Ruby on Rails: Talk

Ariel Juodziukynas

unread,
Jan 15, 2020, 8:41:00 PM1/15/20
to rubyonra...@googlegroups.com
I insist, show your code, show the complete error stacktrace, show the log. I can imagine what you are trying to do and from the (little) code you show it should work so something else is messing things up but you are showing barely any relevant code.

To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1aa0ed3a-a906-4965-8ff1-c8fde1e909da%40googlegroups.com.

fugee ohu

unread,
Jan 15, 2020, 9:01:06 PM1/15/20
to Ruby on Rails: Talk
The error's caused by the routine ensure_canonical_url If I comment it out it works

blog_post.rb:

  def to_param
   "#{id}-#{title.parameterize}"
  end

blog_posts_controller.rb:

    def set_blog_post
      @blog_post = BlogPost.find(params[:id])
    end

    def ensure_canonical_url
      redirect_to @blog_post if @blog_post.to_param != params[:id]
    end

fugee ohu

unread,
Jan 16, 2020, 6:35:31 AM1/16/20
to Ruby on Rails: Talk

So  what do you think is wrong with ensure_canonical_url ?

Ariel Juodziukynas

unread,
Jan 16, 2020, 7:32:35 AM1/16/20
to rubyonra...@googlegroups.com
I guess you are calling ensure_canonical_url before_action BEFORE set_blog_post before_action, so it's not already set.

I guess you have something like:

before_action :ensure_canonical_url, only: :show
before_action :set_blog_post, only: :show

swap both lines if so.

To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3b7d9ae7-2326-4ee1-991f-6e791d3bb35b%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages