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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:28:55+00:00 2026-05-12T13:28:55+00:00

I have an image that belongs_to a user and to a binary (physical file

  • 0

I have an image that belongs_to a user and to a binary (physical file attributes are stored in a separate model). In the old days (read: yesterday), I set the user_id value manually:

@image = Image.new( params[:image] )
@image.user_id = current_user

A friend recently clued me in to the fact that I should use associations instead, so my controller code became:

@image = current_user.image.new( params[:upload] )

That, of course, is much nicer and works great. Now I’d like to do something similar for my binary association, but I don’t know how. Extracting and storing my binary is done by a BinaryObserver class that observes the Image model. In the observer’s before_validation callback, the uploaded file is sent to the Binary class and and a binary object is created/stored. Once stored, I need to set the binary_id value for my image model. I’ve been doing that the manual way, but I now feel like I should be doing it the other way. Here’s what I have in BinaryObserver#before_validation:

def before_validation( model )
  binary          = Binary.new.upload( model.upload ) rescue raise
  binary          = binary.store()
  model.binary_id = binary.id
rescue => e
  #
  # Rethrow any exception that was raised.
  #
  raise
end

Should I be doing this the other way? If so, how? In a multiple association scenario, I can’t see a way to set more than one foreign key value “automagically”.

Thanks.

Rob

  • 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-12T13:28:56+00:00Added an answer on May 12, 2026 at 1:28 pm

    I think I figured out the right answer. Instead of setting a variable to hold the newly created Binary instance, I set the association itself:

    model.binary = Binary.new.upload( model.upload ) rescue raise
    

    That seems to do exactly what I was after. It sets the foreign key value automatically and also allows me to being acting on the associated object directly.

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

Sidebar

Related Questions

So I have a model called Image that belongs_to :user. Each user has a
From the image below. It can be seen that I have three table User,
I have an image that I get and attempt to load into a graphics
I have an image that is too big and I want to put it
I have an image that, depending on the screen resolution, drops down out of
I have an image that I want to resize on my websites rendering using
I have an image that is 480x331 and has a transparent background. The transparent
I have an image that I have in my bundle resources that I need
I have an image that I want to move. I can move the element
So i have an image that has been processed with the sobel method, and

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.