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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:53:00+00:00 2026-05-31T16:53:00+00:00

I was wondering, when you create an object you can often set multiple attributes

  • 0

I was wondering, when you create an object you can often set multiple attributes in a single line eg:

 @object = Model.new(:attr1=>"asdf", :attr2 => 13, :attr3 => "asdfasdfasfd")

What if I want to use find_or_create_by first, and then change other attributes later? Typically, I would have to use multiple lines eg:
@object = Model.find_or_create_by_attr1_and_attr2(“asdf”, 13)
@object.attr3 = “asdfasdf”
@object.attr4 = “asdf”

Is there some way to set attributes using a hash similar to how the Model.new method accepts key-value pairs? I’m interested in this because I would be able to set multiple attributes on a single line like:

 @object = Model.find_or_create_by_attr1_and_attr2("asdf", 13)
 @object.some_method(:attr3 => "asdfasdf", :attr4 => "asdfasdf")

If anyone has any ideas, that would be great!

  • 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-31T16:53:01+00:00Added an answer on May 31, 2026 at 4:53 pm

    You want to use assign_attributes or update (which is an alias for the deprecated update_attributes):

    @object.assign_attributes(:attr3 => "asdfasdf", :attr4 => "asdfasdf")
    
    @object.update(attr3: "asdfasdf", attr4: "asdfasdf")
    

    If you choose to update instead, the object will be saved immediately (pending any validations, of course).

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

Sidebar

Related Questions

I'm wondering how, in JCAPS 6, we can create new eWays. By eWay, I
I'm wondering whether I should create extension methods that apply on the object level
I am wondering how I can create an outer glow effect outside a UIView
Just wondering if anyone has some good resources for learning how to create new
I'm wondering if the 'snapshot' facility of the LevelDB library can create a snapshot
How can I create a String object from a byte array byte arr[MAX_SIZE]; //
Put another way, you create a new kind of object by making a package
I am using php 5.3.6. When I create a new object, the following works:
I was wondering how to: create a droplet that saves to a relative filepath.
Just wondering if it is possible to dynamically create a list of strings in

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.