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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:11:19+00:00 2026-05-19T14:11:19+00:00

Spent all day on Google, but can’t find an answer. :\ I have a

  • 0

Spent all day on Google, but can’t find an answer. :\

I have a HABTM relationship between Users and Core_Values.

class CoreValue < ActiveRecord::Base
  has_and_belongs_to_many :users

class User < ActiveRecord::Base
  has_and_belongs_to_many :core_values

In my controller, I need to do two separate things:

  1. If a CoreValue does not exist, create a new one and associate it with a given user id, and
  2. Assuming I know a particular CoreValue does exist already, create the association without creating any new CoreValues or Users

For # 1, I’ve got this to work:

User.find(current_user.id).core_values.create({:value => v, :created_by => current_user.id})

This creates a new CoreValue with :value and :created_by and creates the association.

For # 2, I’ve tried a few things, but can’t quite seem to create the association ONLY.

Thanks for your help!

  • 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-19T14:11:19+00:00Added an answer on May 19, 2026 at 2:11 pm

    You can do this in a two-step procedure, using the very useful find_or_create method. find_or_create will first attempt to find a record, and if it doesn’t exist, create it. Something like this should do the trick:

    core_value = CoreValue.find_or_create_by_value(v, :created_by => current_user.id)
    current_user.core_values << core_value
    

    Some notes:

    • The first line will find or create the value v. If it doesn’t exist and is created, it will set the created_by to current_user.id.
    • There’s no need to do User.find(current_user.id), as that would return the same object as current_user.
    • current_user.core_values is an array, and you can easily add another value to it by using <<.

    For brevity, the following would be the same as the code example above:

    current_user.core_values << CoreValue.find_or_create_by_value(v, :created_by => current_user.id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SORTED http://drupal.org/node/467190#comment-2068324 Hi friends, I've spent all day to find but can't find :(
I've spent the better part of the day trying to find an answer to
I have spent all day trying to solve this with no satisfying results. I
I have just spent all day attempting to get NHibernate.Search working alongside NHibernate 2.0
Spent all day trying to find ready to use solution for Sort data in
I have spent all Friday and Saturday on this and I am running out
I spent some time looking around, and all I could find is Jython. It's
So I've spent all day looking for good examples of using xpath / xquery
I am pretty new to ASP.NET and C# I have spent the day learning
I spent all day yesterday looking for a text editor for my Asp.Net MVC

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.