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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:13:35+00:00 2026-05-13T00:13:35+00:00

Lets say I have a wine and beer model/controller along with a wine_review and

  • 0

Lets say I have a wine and beer model/controller along with a wine_review and beer_review model with a has many relationship.

Each table has near identical data types, for example, wine_review and beer_review each has a rating and comment column.
Wine and beer each, have names.

How can I dry up my code so they can both share the same views.

Right now I have two views doing the same thing, once for beer and once for wine.

<% @wine_reviews.each do |review| -%>
  <ul>
    <li><%= link_to review.wine.name, review.wine %></li>
    <li><%= review.rating %></li>
    <li><%=h review.comment %></li>
  </ul>
<% end -%>

<% @beer_reviews.each do |review| -%>
  <ul>
  ....
  ....

What I would really like to do is something like

<li><%= link_to review.beverage.name, review.beverage %></li>

Where beverage would be replaced with wine or beer depending on what I’m rendering at that time but I can’t figure out how to do that.

I can’t pass beverage as a local to a partial as review.beverge will break.

Thanks.

  • 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-13T00:13:35+00:00Added an answer on May 13, 2026 at 12:13 am

    You have several different options here:

    Duck typing:

    In Ruby, as long as an object responds to a method call that method call with work. So if both beer and wine have an attribute :name, then calling @result.name will work with either model.

    Polymorphic Associations:

    Instead of having review.beer or review.wine. Make Wine and Beer reviewable via a polymorphic association. This will also work for comments, e.g., both become commentable. Take a look at acts_as_commentable for how this works. Railscasts has an excellent screencast on how this works.

    Single Table Inheritance:

    You could create an object called Beverage from which Wine and Beer inherit. This allows for a single place to put the similar attributes and still allows for small differences.

    From your description, you are looking at a combination of all three possibly.

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

Sidebar

Related Questions

Lets say we have a table with many rows. When transforming my XML to
Lets say we have a table here, populated with the following data: acc_id1 acc_id2
Lets Say i have a table like this WEB_LIST_TABLE KEY Value ---------------------------------------- 134 google.com
Lets say we have a database table with two columns, entry_time and value. entry_time
Lets say i have a table: <table> <tr></tr> </table> $(function(){ $('#btn').click(function(){ $('table').append('<tr><input class=remove value=remove></tr>');
Lets say I have this SQL statements: ALTER TABLE dbo.[tbl] ALTER COLUMN col1 varchar(300)
Lets say I have a table called user_table: user_table id first_name last_name gender email
Lets say I have a controller which provides search functions for a student: public
Lets say you have two classes, Boss and Employee. There is a bidirectional many-to-many
lets say I have a table call payment that have a field call thing_id

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.