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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:33:50+00:00 2026-06-15T15:33:50+00:00

I am modifying a Documents table from using three columns ( article1 , article2

  • 0

I am modifying a Documents table from using three columns (article1, article2, and article3) to one (articles) which has a string of comma-separated IDs stored in it (i.e., 23,4,33,2). That’s all working well, but I’m trying to adjust the functions that read the three columns to read the one and I’m getting rather stuck.

In the model I have:

scope :all_articles, lambda {|p| where(:page => p) }

In the controller I have this:

@articles = (1..3).to_a.map { |i| Article.all_articles(i).reverse }

And in the view:

<% @articles.each_with_index do |a, i| %>
  <%= a[i].name %>
<% end %>

It’s just a bit beyond me at this point.

Cheers!

  • 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-06-15T15:33:51+00:00Added an answer on June 15, 2026 at 3:33 pm

    It’s usually not good practice to put store the ids in a column like you have done. It is better to break that relationship out into a Has and Belongs to Many relationship. You set it up in your models like this:

    class Document < ActiveRecord::Base
      #...
      has_and_belongs_to_many :articles
      #...
    end
    
    class Article < ActiveRecord::Base
      #... 
      has_and_belongs_to_many :documents
    end
    

    Then you will create a join table that ActiveRecord will use to store the relationships.

    create_table :articles_documents, :id => false do |t|
      t.integer :article_id
      t.integer :document_id
    end
    
    add_index :articles_documents, [:article_id, :document_id], unique: true
    

    This will allow you to query a lot more efficiently than you are currently doing. For example, to find all documents that have some article id. You would do:

    @documents = Document.joins(:articles).where("articles.id = ?", some_article_id)
    

    Or if you want to query for a document and return the articles with it:

    @documents = Document.includes(:articles).where(some_conditions)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: XPage with tabbed table (from container controls) and multiple data sources, one for
Modifying CKEditor 3.6.2 is not easy, but I tried hard . One problem that
(Modifying the question)I have a windows form app in C# which calls into some
I am modifying some code for a Blackfin processor using VisualDSP++ v. 5.0. I
I am modifying a Json document through code. The Json has array nested items.
If I have a PHP application which allows users to make changes to documents,
Here's my connection string: sConnection = @Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\ + lstNet.SelectedItem.ToString() + \SharedDocs\Documents\Debug\App_File\ggbase.mdb;Jet OLEDB:Database Password=g3n3r4l;;
I have got python code running on multiple analyzer machines each picking documents from
My intention is to edit HTML documents, including modifying existing elements, deleting elements and
I'm using Railo (pd4ml) with cf_document to print a PDF file, generated from a

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.