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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:35:18+00:00 2026-05-21T11:35:18+00:00

Supposing I have the following action: def index @posts = Post.joins(:tags).where(:tags => {:id =>

  • 0

Supposing I have the following action:

def index
  @posts = Post.joins(:tags).where(:tags => {:id => params[:tag_id]})
end

It exposes @posts to the view, which will display every post with the given tag.

Everything works fine, but I’m stuck trying to figure out the best way to test it.

I don’t really like mocking, since it could brake the test if I changed that line to:

@posts = Post.where(:tags => {:id => params[:tag_id]}).joins(:tags)

I don’t really want to hit the database, as it’d reduce the test speed, but I’m considering extracting the query to a method inside the model, and test it there if it’s the only way to do it.

EDIT: Yes, I know I could use Tag.find(params[:tag_id]) in this case, but this is not what the question is about. I just didn’t want to introduce another model in the query and make it harder to explain deviating the focus from the real problem, which is: Should we keep complex queries in the controller? If so, what’s the best way to test it?

  • 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-21T11:35:18+00:00Added an answer on May 21, 2026 at 11:35 am

    So, according to the comments extracting to the model is the best thing to do. That’s how I did it:

    post.rb:

    class Post
      scope :tagged_as, lambda {|tag_id| where(:tag_id => tag_id)}
    end
    

    posts.yml:

    one:
      title: Post 1
      tags: one, three
    
    two:
      title: Post 2
      tags: two, three
    

    post_test.rb:

    test 'find by tag' do
      posts = Post.tagged_as(tags(:one))
      assert_includes posts, posts(:one)
      refute_includes posts, posts(:two)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposing I have the following: module A class B # ... end # ...
Supposing I have the following script <div id=something> <?php // execute a loop ?>
I have the following code implementation of Breadth-First search. trait State{ def successors:Seq[State] def
Supposing I have the following type from an external library: union foreign_t { struct
Supposing I have the following text: Lorem ipsum dolor sit amet, consectetur adipisicing elit,
What's the object type returned by Datepicker? Supposing I have the following: $(#txtbox).datepicker({ onClose:
Supposing I have the following LambdaExpression: var itemParam = Expression.Parameter(typeof(Thing), thing); var someValue =
Supposing I have the following public class Foo { private Map<Integer,SomeObject> myMap; public Foo()
supposing I have the following data: id name source ----------------- 1 'n' 'a' 1
Supposing I have the following list: A | B | C | D or

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.