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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:05:57+00:00 2026-06-15T16:05:57+00:00

New to Rails and Ruby and trying to do things correctly . Here are

  • 0

New to Rails and Ruby and trying to do things correctly.

Here are my models. Everything works fine, but I want to do things the “right” way so to speak.
I have an import process that takes a CSV and tries to either create a new record or update an existing one.

So the process is 1.) parse csv row 2.) find or create record 3.) save record

I have this working perfectly, but the code seems like it could be improved. If ParcelType wasn’t involved it would be fine, since I’m creating/retrieving a parcel FROM the Manufacturer, that foreign key is pre-populated for me. But the ParcelType isn’t. Anyway to have both Type and Manufacturer pre-populated since I’m using them both in the search?

CSV row can have multiple manufacturers per row (results in 2 almost identical rows, just with diff mfr_id) so that’s what the .each is about

manufacturer_id.split(";").each do |mfr_string|
  mfr = Manufacturer.find_by_name(mfr_string)

  # If it's a mfr we don't care about, don't put it in the db
  next if mfr.nil?

  # Unique parcel is defined by it's manufacturer, it's type, it's model number, and it's reference_number
  parcel = mfr.parcels.of_type('FR').find_or_initialize_by_model_number_and_reference_number(attributes[:model_number], attributes[:reference_number])
  parcel.assign_attributes(attributes)

  # this line in particular is a bummer. if it finds a parcel and I'm updating, this line is superfulous, only necessary when it's a new parcel
  parcel.parcel_type = ParcelType.find_by_code('FR')

  parcel.save!
end


class Parcel < ActiveRecord::Base
  belongs_to :parcel_type
  belongs_to :manufacturer

  def self.of_type(type)
    joins(:parcel_type).where(:parcel_types => {:code => type.upcase}).readonly(false) unless type.nil?
  end
end


class Manufacturer < ActiveRecord::Base
  has_many :parcels
end


class ParcelType < ActiveRecord::Base
  has_many :parcels
end
  • 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-15T16:05:58+00:00Added an answer on June 15, 2026 at 4:05 pm

    It sounds like the new_record? method is what you’re looking for.

    new_record?() public

    Returns true if this object hasn’t been saved yet — that is, a record
    for the object doesn’t exist yet; otherwise, returns false.

    The following will only execute if the parcel object is indeed a new record:

    parcel.parcel_type = ParcelType.find_by_code('FR') if parcel.new_record?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to ruby/rails and trying to build a simple Projects / Tags app
I am new to Ruby on Rails and I am trying to make a
I'm trying to get Ruby on Rails setup on my new eeeubuntu install and
I'm new to Ruby on Rails and I'm trying to model my table relationships.
I'm new to Ruby on Rails, and I'm trying to create a bass guitar
When trying to run a new refinerycms application with ruby 1.9.2 rails 3.2.1 and
I'm new in Ruby/Rails, so, my question: what is the perfect way to upload
I am new to Ruby On Rails, and currently trying to modify an existing
I'm a new ruby/rails user trying to get my mac set up for the
I'm super new to Ruby on Rails. I'm trying to make an authentication system

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.