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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:06:47+00:00 2026-05-31T11:06:47+00:00

I might be doing this wrong overall, but maybe someone will be able to

  • 0

I might be doing this wrong overall, but maybe someone will be able to chirp in and help out.

Problem:

I want to be able to build a relationship on an unsaved Object, such that this would work:

v = Video.new
v.title = "New Video"
v.actors.build(:name => "Jonny Depp")
v.save!

To add to this, these will be generated through a custom method, which I’m attempting to modify to work, that does the following:

v = Video.new
v.title = "Interesting cast..."
v.actors_list = "Jonny Depp, Clint Eastwood, Rick Moranis"
v.save

This method looks like this in video.rb

def actors_list=value
  #Clear for existing videos
  self.actors.clear

  value.split(',').each do |actorname|
    if existing = Actor.find_by_name(actorname.strip)
      self.actors << existing
    else
      self.actors.build(:name => actorname.strip)
    end
  end
end

What I expect

v.actors.map(&:name)
=> ["Jonny Depp", "Clint Eastwood", "Rick Moranis"]

Unfortunatey, these tactics neither create an Actor nor the association. Oh yeah, you might ask me for that:

in video.rb

has_many :actor_on_videos
has_many :actors, :through => :actor_on_videos

accepts_nested_attributes_for :actors

I’ve also tried modifying the actors_list= method as such:

def actors_list=value
  #Clear for existing videos
  self.actors.clear

  value.split(',').each do |actorname|
    if existing = Actor.find_by_name(actorname.strip)
      self.actors << existing
    else
      self.actors << Actor.create!(:name => actorname.strip)
    end
  end
end

And it creates the Actor, but I’d rather not create the Actor if the Video fails on saving.

So am I approaching this wrong? Or have I missed something obvious?

  • 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-31T11:06:49+00:00Added an answer on May 31, 2026 at 11:06 am

    Try this:

    class Video < ActiveRecord::Base
    
      has_many :actor_on_videos
      has_many :actors, :through => :actor_on_videos
    
      attr_accessor :actors_list
      after_save    :save_actors
    
      def actors_list=names
        (names.presence || "").split(",").uniq.map(&:strip).tap do |new_list|
          @actors_list = new_list if actors_list_changes?(new_list)
        end
      end
    
      def actors_list_changes?(new_list)
        new_record? or 
          (actors.count(:conditions => {:name => new_list}) != new_list.size)
      end
    
      # save the actors in after save.
      def save_actors
        return true if actors_list.blank?
        # create the required names
        self.actors = actors_list.map {|name| Actor.find_or_create_by_name(name)}
        true
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I might be doing this wrong, if I am, let me know, but I'm
I might be going about this in totally the wrong way but this is
I must be doing something really wrong, but I can't find anything on this
Be aware that I might be doing this totally wrong, so I apologize in
I might be doing something completely wrong here but for some reason the values
I might be doing this the wrong way when I set up the tables?
I can't seem to get a custom action working. I might be doing this
I'm new to Java Web Services, so I might be doing things wrong. I'm
Okay, so it seems, for some reason (I might be doing it wrong, obviously),
I can't see what I'm doing wrong. I think it might be one of

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.