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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:20:42+00:00 2026-06-14T14:20:42+00:00

I have a Publication model with a has_many relationship to Contributors. In the model,

  • 0

I have a Publication model with a has_many relationship to Contributors. In the model, I have a method that’s meant to create an html-ready by line:

def authors
  authors = []
  Contributor.where(:publication_id => self.id).each do |author|
    authors << "link_to "+author.name+", Contributor.find("+author.id.to_s+")"
  end
  authors.to_sentence
end

In my view, I have the following line:

by <%= @publication.authors %>

But instead of rendering links, it renders the raw code, such as:

by link_to B, Contributor.find(1)

I’ve tried patching this by adding .html_safe to the end of @publication.authors, but to no avail. Is there a better way to transfer these links from the model to the view?

  • 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-14T14:20:43+00:00Added an answer on June 14, 2026 at 2:20 pm

    You’re pushing strings into your authors array. It looks like valid code, so running eval on it should work. (Actually author.name will probably evaluate as an undefined symbol, so scratch that.)

    A better way would be to use a has_many :authors, :model => 'Contributor' relationship on your Publication model, and you can bring up your array of Contributor objects by simply calling

    @publication.authors
    

    You’d want to iterate over these in your view like so:

    <% @publication.authors.each do |author| %>
      <%= link_to author.name, author %>
    <% end %>
    

    Note also that if you’re displaying multiple Publication objects in a view this way, you’ll want to use Publication.includes(:authors) in your controller when you’re retrieving them to avoid the “N+1” problem.

    Now, three lines of code doesn’t seem very expensive to repeat, but there are ways to DRY that without violating the MVC pattern and cluttering your model:

    • Place the code to print a publication’s authors into a partial, and call the partial as needed.
    • Place the code into a helper, include the helper and call the method as needed.

    Here’s a snippet from the source for to_sentence (you can adapt it for your needs, I think):

    case length
      when 0
        ""
      when 1
        self[0].to_s.dup
      when 2
        "#{self[0]}#{options[:two_words_connector]}#{self[1]}"
      else
        "#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
    end
    

    The full source can be found here.

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

Sidebar

Related Questions

I am trying to model a publications. A publication can have multiple authors and
I have the following models, with publication needing a m2m with authors via the
I have the following setup: class Publication < ActiveRecord::Base has_and_belongs_to_many :authors, :class_name=>'Person', :join_table =>
I have the following two models class Author(Model): name = CharField() class Publication(Model): title
I have a Drupal view that lists a node called publication. At the top
I have a small application that searches publication citations. The citations are in XML
I have two models for Publications and Employees: class Publication(models.Model): BOOK_CHAPTER = 1 ARTICLE
Got a question. Say I have two models in a many-to-many relationship (Article, Publication).
In my table, I have a column that is publicationDate , publication date set
I have an app that displays news stories for a publication. If a user

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.