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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:15:47+00:00 2026-05-23T14:15:47+00:00

I have the two following models associated: class Post < ActiveRecord::Base belongs_to :language end

  • 0

I have the two following models associated:

class Post < ActiveRecord::Base
  belongs_to :language
end

class Language < ActiveRecord::Base
  has_many :posts
end

From a view I have a link to filter the posts by language:

<div id="english"><%= link_to "English", {:controller => 'posts', :action => 'search_result', :language => "english"} %></div>

The model language has a variable name:string which is the one i am using to make the active record query.

The doubt i have is how i can make this query from the post controller to retrieve the right posts which has a field: language.name == “english”.

I tried this:

@posts = Post.all(:conditions => ["language.name = ?", params[:language]])

and also this:

@posts = Post.where(:language.name => params[:language])

Hope i have explained well the issue, i am a quite newbie yet. Ah! i would also know what would it be better in this case to use: “all” or “where” ??.
Thanks a lot in advance.

  • 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-23T14:15:48+00:00Added an answer on May 23, 2026 at 2:15 pm

    You need to do a join: http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables

    If I have understood your models/database structure correctly, the ActiveRecord call should looks something like:

    Post.joins(:language).where('languages.name' => params[:language])
    

    Hope that helps.

    PS. The where call is the preferred method these days.

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

Sidebar

Related Questions

I have the following two models class Post < ActiveRecord::Base has_many :comments end class
I have the following two models: class PhotoAlbum < ActiveRecord::Base belongs_to :project has_many :photos,
I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I have two models, Page and PageContent. class Page < ActiveRecord::Base has_many :page_contents end
I have the following two models: class Message < ActiveRecord::Base belongs_to :to_user, :class_name =>
I have two models like this: class Topic < ActiveRecord::Base has_many :articles end class
I have the following models in my Rails application: class Shift < ActiveRecord::Base has_many
I have the following two classes: class Menu < ActiveRecord::Base has_many :menu_headers accepts_nested_attributes_for :menu_headers
I have the following models: class Match < ActiveRecord::Base has_and_belongs_to_many :teams end And class
I have the following two models: Project, has_many ProjectParticipants ProjectParticipants, belongs_to Project How can

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.