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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:41:30+00:00 2026-05-26T13:41:30+00:00

How can I do a cross-model find in a way which conforms most closely

  • 0

How can I do a cross-model find in a way which conforms most closely to the MVC pattern?

I have a model, Istc.rb. In the index view that corresponds to this model, I want to do this:

   = "Last edited by #{User.find_by_id(istc.user_id).first_name}"    

Clearly you can’t have a model.find in a view – and not only is this a model.find, it’s a find on a different model. But how would you refactor this?

I’m nervous about having a helper like this because it’s still pretty close to the View layer:

module UsernameHelper
  def user_name(model)
    User.find_by_id(model.user_id) unless User.find_by_id(model.user_id).nil?
  end
end

Is the way to do it to have a scope on User?

# User.rb
scope :lookup, lambda {|model| where("id = ?", model.user_id)}

# username_helper.rb
module UsernameHelper
  def user_name(model)
    User.lookup(model).first unless User.lookup(model).first.nil?
  end
end

Or should the istcs_controller handle it? Or should there be a separate controller, which talks to both the Istc and User model?

Thoughts welcome, and I’d really like to see any sample applications which solve this sort of problem elegantly, too.

  • 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-26T13:41:30+00:00Added an answer on May 26, 2026 at 1:41 pm

    As Rasmus said, if we make use of association its a simple think,
    User Model:

    class User < ActiveRecord::Base
       has_many :istcs, :class_name => "Istc"
    end
    

    Istc Model:

    class IStc < ActiveRecord::Base
      belongs_to :user
    end
    

    view:

    <%= "Last edited by #{istc.user.first_name}" %>
    

    If you are not happy with this the following would be the alternate solutions,

    1. Write a helper method in application_herlper.rb(Since you are using in different controller views.
    2. Write a instance method in Istc.rb model, that you could call from one the istc object directly from the view.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I find the user's home directory in a cross platform manner in
we have this application which uses cross app domain (2 app domains in the
I'm working on a WPF application and is using the Model-View-ViewModel pattern. The application
I have created a View Model in Silverlight. This View Model has an event
Can we make cross browser css layouts with CSS positioning, without using float? What
How can I implement cross browser opacity gradient (not color gradient)? See following code:
How can I make a cross-platform emacs command that opens another instance of emacs
How can I get the list of cross product pairs from a list of
I'm looking for a cross-platform database engine that can handle databases up hundreds of
I am looking for a cross platform Library in C++ that can run 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.