Skip to content

tamouse/example_questions_without_answers

Repository files navigation

Example: How do I find questions with no answers

Two models:

  • Question
  • Answer

With relationships:

  • Question has_many Answers
  • Answer belongs_to Question

how to find empty relationships:

Question.includes(:answers).where(answers: {id: nil})

how to find questions that have answers:

Question.includes(:answers).where.not(answers: {id: nil})

implementation as scopes:

Rails scopes provide the means to make this a bit more readable:

scope :with_no_answers, ->{
  includes(:answers).where(answers: {id: nil})
}

scope :with_answers, ->{
  includes(:answers).where.not(answers: {id: nil})
}

Then you can use the scopes instead of the long expressions previously:

@unanswered = Question.with_no_answers

See app/models/question.rb and spec/models/question_spec.rb for details.

Using this repo/Contributing

This rails app is not intended for production. There is nothing configured for production, and no production.rb in environments.

Also, the only features included are ActiveModel and ActiveRecord. No mailers, views, jobs, controllers, sprockets, test_unit. RSpec is used for running tests.

SQLite3 is adequate to show the relationships above and run the tests.

Setup

$ bundle install
$ bundle binstub rspec-core
$ bin/rake db:setup # may need db:drop first if already installed

Console

Pry is the console of choice!

Tests

$ bin/rake

Contributing

The usual mantra "fork it/branch it/write it/push it" should be followed. :)

  1. Fork https://github.com/tamouse/example_questions_without_answers.
  2. Make a branch for your changes.
  3. Edit and test your changes.
  4. Push your changed branch back to your repo.
  5. Open a Pull Request to this repo.

Also, feel free to contribute to the project wiki and ask questions on the project issues.

Author

Tamara Temple is a geeky girl, webologist and software craft maven. I like to try things out, learn stuff, and then try to help others learn, too. My dev blog is at http://swaac.tamouse.org and I'm on github as tamouse.