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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:14:22+00:00 2026-06-12T09:14:22+00:00

class QuestionGroup < ActiveRecord::Base has_many :questions end class Question < ActiveRecord::Base belongs_to :question_group has_many

  • 0

class QuestionGroup < ActiveRecord::Base
  has_many :questions
end

class Question < ActiveRecord::Base
  belongs_to :question_group
  has_many :question_answers
  has_many :question_users_answers, :through => :question_answers, :source => :user_question_answers

  def self.questions_without_answers(user_id)
    select {|q| q.question_users_answers.where(:user_id=>user_id).empty?}
  end
end

class QuestionAnswer < ActiveRecord::Base
  belongs_to :question
  has_many :user_question_answers
end

I need find all Questions if they have no user answers I did it by class method self.questions_without_answers(user_id)

But how can I find all QuestionGroups where present questions_without_answers and for particular user?

P.S: I need to find all unanswered questions and all groups that own these questions, can I do it by find or named-scope?

UPDATED:

  def self.groups_without_answers(user_id)
    questions_ids = Question.questions_without_answers(user_id).map {|q| q.id}
    all(:conditions => "id in (select distinct question_group_id from questions where id in (#{questions_ids.join(',')}))")
  end

But I think it is not good or maybe I wrong?

  • 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-12T09:14:23+00:00Added an answer on June 12, 2026 at 9:14 am
    class QuestionGroup < ActiveRecord::Base
      has_many :questions
    
      def self.without_answers(user_id)
        joins(%"inner join questions on question_groups.id = questions.question_group_id
                inner join question_answers
                  on question_answers.question_id = questions.id
                inner join question_groups
                  on question_answers.question_users_answers_id = question_users_answers.id").where("user_question_answers.user_id" => user_id).select { |qq| ... }
        end
      end
    end
    

    You can change some of the inner joins to left out join to pick up records where the table you are joining to doesn’t have a match, for instance where there is no answer. The fields of the table you are joining to will have NULL values for all the fields. Adding a where id is null will even filter to just the questions with no answers.

    Keep in mind that this is just an alternative technique. You could programmatically solve the problem simple with:

    class QuestionGroup
      def self.question_groups_without_answers(user_id)
        select {|qq| qq.question_users_answers.where(:user_id=>user_id).empty?}.map{ |qq| qq.question_group }
      end
    end
    

    An advantage of doing the joins is that the database does all the work, and you don’t send several SQL queries to the database, so it can be much faster.

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

Sidebar

Related Questions

class Client < ActiveRecord::Base has_many :hosts, :dependent => :destroy end class Host < ActiveRecord::Base
class Person < ActiveRecord::Base validates :terms_of_service, :acceptance => true end In the above, what
class Anketum < ActiveRecord::Base has_one :user class << self def search(params) self.scope :h, :conditions
class Subscription < ActiveRecord::Base validates :feed_id, presence: true, uniqueness: true now that I verify
class A attr_accessor :dab .... end Now I have an array of instances of
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
class CartesianProduct include Enumerable # your code here end #Examples of use c =
class ABC is an abstract base class. class X is its subclass. There's some
class CreateMatches < ActiveRecord::Migration def self.up create_table :matches do |t| t.integer :result_home t.integer :result_away
class Widget; std::vector< std::shared_ptr<Widget> > container class Criterium { public: bool operator()(const Widget& left,

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.