Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:07:58+00:00 2026-05-31T09:07:58+00:00

In Rails 3, how can I retrieve a record by looking up on a

  • 0

In Rails 3, how can I retrieve a record by looking up on a virtual attribute? I have a name column and a slug that does a simple modification to it (see code below). How can I do a “reverse lookup” by slug to name?

I’m trying to find the right code for the find_by_slug method below:

class Brand < ActiveRecord::Base
  has_many :sale_items
  validates :name, :uniqueness => true

  def slug
    self.name.downcase.gsub(/\s/, '-')
  end

  def self.find_by_slug(given_slug)
    first(slug: given_slug)
  end
end

When I try find_by_slug in the Rails console I’m given an Unknown key: slug error.

This question seems similar but I’m not sure if it’s the same as my problem. I’d appreciate some insight and help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:08:00+00:00Added an answer on May 31, 2026 at 9:08 am

    Since slug doesn’t exist in the database you can’t query for it with first. What you need to do is search on the name. But in order to do that, your ‘name to slug’ mapping needs to be reversible. You need to be able to do

    name = 'San Francisco'
    slug = convert_to_slug(name)  #san-francisco
    name == convert_to_name(slug) #true
    

    So that given a slug, you know what it’s name would be – you can then do find_by_name. Right now your slug conversion

    downcase.gsub(/\s/, '-')
    

    is not reversible, unless there’s some implicit information that you’re not sharing with us, like ‘there are only spaces and every first letter is capitalized’. So it can’t be done, and you’re best off making this a regular attribute and not a virtual one. If your conversion is reversible, then you just have to use

    find_by_name(convert_to_name(slug))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for an upload solution for Ruby on Rails where I can
In Rails 2.x you can use validations to make sure you have a unique
Rails 2.3.3 Database and Model Schema User (id,name,email) posts (id,subject,message,user_id) How can I display
I have a rails application that serves as an interface to a hybrid of
I have a rails 3 app that creates a sessions with: cookies.permanent.signed[:remember_token] = [user.id,
I have a model in a Rails/Mongoid application that I had initially set a
I have what seems like a simple problem in my Rails 3.1 app: I
Can rails handle creating a view without a controller? For example, let say I
A single Rails command can make lots of changes in an app - creating
Hoping some learned Rails developers here can recommend an existing Ruby on Rails plugin

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.