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

  • SEARCH
  • Home
  • 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 7943061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:09:38+00:00 2026-06-04T00:09:38+00:00

what am I doing wrong here? I have a model for an app I

  • 0

what am I doing wrong here?

I have a model for an app I am writing called page. Those attributes are:
title
pagetype
page_url

title and pagetype can be set as normally, but I used a custom getter/setter for the page_url. Here is the logic/model:

class Page < ActiveRecord::Base

def page_url=()
    temp = self[:title]
    pageUrl = temp.gsub(" ", "_").downcase  
if self[:pagetype] == "home"
        pageUrl = "/"
    end
    self[:page_url] = pageUrl
end

def page_url
    self[:page_url]
end

end

It’s fairly simple -> page_url is based on the title with all spaces replaced with unless page_type == “home”, which then gets set to “/”. For the record I don’t want to make page_url virtual because I need it to be searchable and saved in the db.

So unfortunately whether in rails console or my app this is failing. Here is how I am calling the setter method in the console;

page1 = Page.new
page1.pagetype = "home"
page1.title = "this is a test"

page2 = Page.new
pager2.pagetype = "content"
page2.title = "this is another test"

#expected results should be
page1.page_url() 
=> "/"
page2.page_url()

However I keep getting this:

page1.page_url()
=> nil

What the heck am I doing wrong here?

  • 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-06-04T00:09:39+00:00Added an answer on June 4, 2026 at 12:09 am

    These custom setter and getters don’t persist to the database. If you have a column page_url in your database, you can set the value with a callback. E.g. before_save:

    class Page < ActiveRecord::Base
      before_save :set_page_url
    
      def set_page_url
        if self[:pagetype] == "home"
          self.page_url = "/"
        else
          self.page_url = self[:title].gsub(" ", "_").downcase
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure what I'm doing wrong here, I have a file in lib/acts_as_votable.rb
I don't know what I'm doing wrong here. I have a ListBox whose DataContext
What am I doing wrong here folks? <?php include 'header.php'; /** * Display a
What am I doing wrong here? I'm trying to get started with jQuery UI
What am I doing wrong here: class Helo { // main: generate some simple
What am I doing wrong here? I'm serializing a value, storing it in a
What am I doing wrong here? private void SendMail(string from, string body) { string
What am I doing wrong here? Im trying to save a canvas drawing by
What am I doing wrong here? It says Database Count Failed (on the $count
What am I doing wrong here? Declare @starttimestamp datetime = getdate(); RAISERROR(N'Code not valid

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.