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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:25:44+00:00 2026-05-28T04:25:44+00:00

I have three models Article , Author , and AuthorLine presenting relationship between article

  • 0

I have three models Article, Author, and AuthorLine presenting relationship between article and its authors, in a many-to-many mapping.

class Article < ActiveRecord::Base                                                                                                                                                                
  has_many :author_lines, :dependent => :destroy                                                                                                                                                                          
  has_many :authors, :through => :author_lines, :dependent => :destroy, :order => 'author_lines.position'   

  attr_accessor :author_list
end

class Author < ActiveRecord::Base                                                                                                                                                                 
  has_many :author_lines                                                                                                                                                                          
  has_many :articles, :through => :author_lines                                                                                                                                                   
end

class AuthorLine < ActiveRecord::Base                                                                                                                                                             
  validates :author_id, :article_id, :position, :presence => true

  belongs_to :author, :counter_cache => :articles_count                                                                                                                                           
  belongs_to :article                                                                                                                                                                             
end

The AuthorLine model has an additional attribute position, which tells the order of authors for an article.

Here is what I am doing to create an article with given author names, in article.rb:

def author_list=(raw)                                                                                                                                                                           
  self.authors.clear                                                                                                                                                                            
  raw.split(',').map(&:strip).each_with_index do |e, i|                                                                                                                                         
    next if e.blank? 
    author = Author.find_or_create_by_name(e)                                                                                                                                                   

    #1                                                                                                                                                                      
    self.authors << author                                                                                                             

    #2
    # AuthorLine.create(:author_id => author.id, :article_id => self.id, :position => i)                                                                                                        
  end                                                                                                                                                                                           
end

The problem is I have no idea when to update the position attributes of corresponding AuthorLines. if I remove the line #1 and uncomment the line #2, the created AuthorLine may have a nil arctile_id since self.id may not be given.

  • 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-28T04:25:44+00:00Added an answer on May 28, 2026 at 4:25 am

    I’d probably move the code for creating AuthorLines to an after_create hook in your Article model. If I understand the problem correctly, something like this should do the trick:

    after_create :set_author_line_positions
    
    def set_author_line_positions
      self.authors.each_with_index do |author, index|
        existing_author_line = AuthorLine.where("author_id = ? and article_id = ?", author.id, article.id).first
        if existing_author_line
          existing_author_line.update_attributes(:position => index)
        else
          AuthorLine.create(:author_id => author.id, :article_id => self.id, :position => index)
        end
      end
    end
    

    That way, you only wind up setting the AuthorLine positions after your article is already created and has an ID. This also checks to make sure an AuthorLine has already been created; I believe that an AuthorLine would get created every time an author is added to an article but I like to have very explicit checks in callbacks like this.

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

Sidebar

Related Questions

I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
Got a question. Say I have two models in a many-to-many relationship (Article, Publication).
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I currently have this chunk of model code: class Book(models.Model): title = models.CharField(max_length=100) num_pages
I have probably stupid question but how Can I create Many-To-Many relationship with create
I have a join table with three columns. I've tried defining the three models

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.