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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:51:13+00:00 2026-06-02T16:51:13+00:00

I have a simple Rails 3 application that has a number of models. A

  • 0

I have a simple Rails 3 application that has a number of models. A simple overview of the models I’m having trouble with is:

client model

has_many :animals

animal model

belongs_to :client

What I would to be able to do is show a list of other animals that are owned by the same client.

Something like this:

<% @client.animals.each do |animal| %>
  <%= animal.AnimalName %>
<% end %>

As this is within the Animal controller, my example code won’t work. Any pointers would be appreciated.

Update

To clarify, if I have the following records:

Danny (Client)
Cat (animal owned by Danny)
Dog (animal owned by Danny)
Rabbit (animal owned by Danny)

and I then went to the show view of the Dog’s record, I would like a list that would show all animals that Danny owned. E.g.

Cat
Dog
Rabbit

Ideally excluding the currently viewed animal (in this case dog).

I have tried the following but it doesn’t seem to work:

<% @client.animals.each do |client| %>
  <%= @client.animal.AnimalName %>
<% end %>
  • 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-02T16:51:15+00:00Added an answer on June 2, 2026 at 4:51 pm

    If I understand you correctly you have @animal and want to show all the animals owned by the owner of @animal. This can be done like this:

    <% @animal.client.animals.each do |animal| %>
      <%= animal.AnimalName %>
    <% end %>
    

    Update:

    You can just add a .where onto @animal.client.animals:

    @animal.client.animals.where('id != ?', @animal.id).each ...
    

    It’s not such a good idea to do this in a view. So I would add an instance method to my Animal model:

    def other_animals_with_same_owner
      client.animals.where('id != ?', id)
    end
    

    With this you can do:

    @animal.other_animals_with_same_owner.each ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails simple application that has two main models. A person model
I have a Rails 3 simple scaffolded application having one model like that: class
I have created a rails application that has a simple RESTful json API. This
I have a really simple Rails application that allows users to register their attendance
I have created a simple API for a Rails application using token-based-authentication that supports
I have a Ruby on Rails application that has two active environments, Stage and
I have a simple rails app with models project and phase. A project has
I have a fairly simple Rails application that allows users to manage their clients
What's the quickest / easiest starting point for a simple Rails application that has
In my Rails application I have a simple controller which also has an action

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.