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 4330096
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:54:56+00:00 2026-05-21T09:54:56+00:00

I have the following in my model: before_validation :update_temp attr_accessor :temp_int, :temp_dec def update_temp

  • 0

I have the following in my model:

before_validation :update_temp

attr_accessor :temp_int, :temp_dec

def update_temp

    self.temp = temp_int.to_f + (temp_dec.to_f / 10)

end

And I have the following in my view:

<%= f.select(:temp_int, ["97", "98"], { :include_blank => true })%> . 

<%= f.select(:temp_dec, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], { :include_blank => true })%>

This works great for saving the temperature values. However, there’s one issue. Normally on the edit screen, the select helper would have the existing values pre-selected. But using the virtual attributes this isn’t happening.

How do I make sure that the temp_int and temp_dec values get passed to the view?

  • 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-21T09:54:57+00:00Added an answer on May 21, 2026 at 9:54 am

    The view should have no problem accessing the attributes, but have the attributes been initialized within the view’s action? It looks like, if you’ve just loaded an instance of the model, those attributes are still nil, regardless of the value of #temp.

    Note that if your action redirects, the browser will make a new connection to access the subsequent action, and will not have access to the model instance that was use in the original action.

    Perhaps, you need to add something like the following:

    after_initialize :init_temp_parts
    
    def init_temp_parts
      return if temp.blank?
      @temp_int = temp.to_i
      @temp_dec = temp - @temp_int
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following in the model: before_save :set_defaults def set_defaults self.num_results ||= 5
I have the following view model: public class MyViewModel { public bool SingleFamily {
I have the following code in my user model: before_save :create_remember_token private def create_remember_token
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have the following model: class User: name = models.CharField( max_length = 200 )
I have the following model association: a student model and has_many scores. I need
I have the following model setup - a User is interested in projects in
I have the following Model and Controller files, and when i visit this url,

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.