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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:09:43+00:00 2026-06-14T17:09:43+00:00

I have the following DB for a simple flash cards example i’m building: create_table

  • 0

I have the following DB for a simple flash cards example i’m building:

create_table "card_associations", :force => true do |t|
  t.integer  "card_id"
  t.integer  "deck_id"
end

create_table "cards", :force => true do |t|
  t.string   "question"
  t.string   "answer"
end

create_table "decks", :force => true do |t|
  t.string   "name"
  t.string   "description"
end

I’ve setup the has_many through relationships in all my models.
Now I want to be able to return a list of all cards from the join table, given the deck id.
If I run the query:

CardAssociation.find_by_deck_id(3).card

It retruns the first card with the deck_id of 3. But when I try.

CardAssociation.find_all_by_deck_id(3).card

I get the error

NoMethodError: undefined method `card’ for #

Can someone help me with this? I feel like i’m making a very simple mistake.

Thanks for the help

  • 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-14T17:09:43+00:00Added an answer on June 14, 2026 at 5:09 pm

    The find_all_* methods always return an Array (which could be empty)!

    CardAssociation.find_all_by_deck_id(3) # => Array of results
    CardAssociation.find_all_by_deck_id(3).first # => first result of the Array or nil if no result
    

    I advise you to first read the Ruby on Rails Style Guide, and then use the Rails3 way of finding object with ActiveRecord:

    CardAssociation.where(:deck_id => 3) # => Array of results
    CardAssociation.where(:deck_id => 3).first # => first result of the Array if exists
    

    In your case, a scope can be set up on the Card model:

    You said: “Now I want to be able to return a list of all cards from the join table, given the deck id“

    class Card < ActiveRecord::Base
      scope :for_deck, lambda { |deck| joins(:card_associations).where('card_associations.deck_id = ?', deck.try(:id) || deck) }
    end
    

    This scope can be used like following:

    Card.for_deck(deck) # returns an Array of Card objects matching the deck.id
    

    As defined in the scope, the parameter of Card.for_deck(deck) can be a deck object or a deck_id (type Integer)

    Hope this helped!

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

Sidebar

Related Questions

I'm using Flash Builder 4.6. As a simple example, say I have the following
Hi i have created a simple menu in Flash. I have added the following
In frame 1 of a very simple flash file, I have the following code:
I have the following simple object embed for Flash: <object id=siwp_obj type=application/x-shockwave-flash data=button.swf?file=%2F/ex.php?id=89804c7326e92a00c5c88c181190af8159cf060b height=32
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I'm using MVC 4, EF 4.3 and the MVCScaffolding package. I have following simple
I have following rather simple query select count(*) from tbl t1, tbl t2 For
I have a following simple code: def get(): return [lambda: i for i in
I have the following simple class definition: //mycommon.h @interface CurrentPath : NSObject @property (nonatomic,

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.