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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:53:38+00:00 2026-05-18T06:53:38+00:00

I have a question regarding many-to-many relationships, specifically has_many :through associations. All the tutorials

  • 0

I have a question regarding many-to-many relationships, specifically has_many :through associations.

All the tutorials I found just set you up with the models and migrations but leave you hanging when it comes to controllers.

What I want to do is to update the timestamps of the join table when an article that is already present is added again, so that it moves to the top of the “list”. How do I do that?

This is what my create action looks like:

def create
  @article = Article.find_or_create_by_url(params[:article])
  if current_user.articles.find(@article)

      # update the timestamps on the join table 
      # ie moved the old article to the top

      flash[:notice] = "Successfully added article (again)."
      redirect_to @article
  else
    @article.users << current_user
    if @article.save
      flash[:notice] = "Successfully added article."
      redirect_to @article
    else
      render :action => 'new'
    end
  end
end

Thanks in advance!

Update:

@Ben Lee

Thanks for your answer, as I have a has_many through association my article model looks like this:

has_many :readinglist_items, :dependent => :destroy
has_many :users, :through => :readinglist_items

So I don’t know if I can add a :touch => true to has_many as I just want to specific entry in the join table.

The point of the update in the create action is to move the article to the top (instead of adding it again) if a user adds an article it have already added in the past.

  • 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-18T06:53:38+00:00Added an answer on May 18, 2026 at 6:53 am

    I solved it! (readinglist_item is the name of the join table):

    def create
      @article = Article.find_or_create_by_url(params[:article])
      if current_user.articles.find(@article)
    
          @article.readinglist_items.find_by_user_id(current_user.id).touch
    
          flash[:notice] = "Successfully added article (again)."
          redirect_to @article
      else
        @article.users << current_user
        if @article.save
          flash[:notice] = "Successfully added article."
          redirect_to @article
        else
          render :action => 'new'
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I have a question regarding multithreading. First off, how many instances of DispatcherServlet
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
I have a question regarding an update function I created... CREATE OR REPLACE FUNCTION
I have a question regarding handling errors in a J2EE application. Our current application
I'm writing a small webapp in Grails and I have the following question regarding
Following on from my recent question regarding parsing XML files in Java I have
I have a question about best practices regarding how one should approach storing complex
I have a simple question and wish to hear others' experiences regarding which is
I have a question regarding a rather advanced DataModel which I would like to
Question regarding how to setup dbase relationships (newbie, this may be trivial) Followed the

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.