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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:47:37+00:00 2026-05-26T10:47:37+00:00

I have a model named UserPrice which has a form where you can create

  • 0

I have a model named UserPrice which has a form where you can create many UserPrice's at once. I have this virtual attribute called :all_dates which is suppose to update another date_select field of my UserPrice model called :purchase_date but since it isn’t doing its job correctly I need a detailed explanation on this method here so I can get it to work:

def save_all_dates_to_user_prices
 if !self.all_dates.nil?
 self.user_prices.each {|up| up.purchase_date = self.all_dates if up.new_record?}
 end
end

I’ll start out:

  1. I defined the method save_all_dates_to_user_prices to happen
    before_save in my UserPrice model.

  2. if !self.all_dates.nil? means the UserPrice.all_dates attribute is being check if it is blank or not there (nil?).

This is where I get lost; not being sure on this line:

self.user_prices.each {|up| up.purchase_date = self.all_dates if up.new_record?}
  • Its wrapping each UserPrice.user_prices? It wouldn’t look like this if I am trying to get an Array of new records right?
  • Why use |up|, is that the self.user_prices.each stands for?
  • self.user_prices.each = anything wrapped in the {} (hashes)?

Along with answering the questions I have above could someone fill/correct the details for me about this method?

Thanks, I am new to Rails and Ruby trying to learn as I code.

  • 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-26T10:47:38+00:00Added an answer on May 26, 2026 at 10:47 am

    each is a method of the Enumerable class, which both Array and Hash implement. It takes a block as an argument and simply applies it to all the elements of the Enumerable. So the line you’re asking about translates like this:

    for each for the user_prices, assign all_dates to purchase_date if it’s a new user_price

    The up is just a variable referring to the current Enumerale element.

    Here’s a good explanation of closures in ruby.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model named UserPrice which has the attribute :purchase_date (a date_select) in
Hello I have a model named Client which has nested models called Receiver and
I have a model named Domain which looks like this: class Domain(models.Model): Model for
In my model named Profile_model I have this function which retrieves profile data for
Let's say I have this model named Product with a field named brand .
I have a controller/model hypothetically named Pets. Pets has the following declarations: belongs_to :owner
I am using a virtual attribute called :all_dates on my form . The point
I have a relatively simple problem. I have a model named Item which I've
sorry for my english, I have a model named Recipe and Recipe has an
I have model called Search and a resource named :search . I'd like to

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.