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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:06:07+00:00 2026-06-18T04:06:07+00:00

I have some model classes like this: class Organisation < ActiveRecord::Base has_many :dongles has_many

  • 0

I have some model classes like this:

class Organisation < ActiveRecord::Base
  has_many :dongles
  has_many :licences_on_owned_dongles, :through => :dongles, :source => :licences,
           :include => [:organisation, :user, :owner_organisation, :profile, :dongle,
                        {:nested_licences => [:profile]} ]
end

class Dongle < ActiveRecord::Base
  has_many :licences
  belongs_to :organisation
end

class Licence < ActiveRecord::Base
  belongs_to :dongle

  # tree-like structure. I don't remember why this had to be done but the comment says
  # "find a way to make the simpler way work again" and I tried using the simpler way
  # but tests still fail. So obviously the SQL awfulness is necessary...
  default_scope :conditions => { :parent_licence_id, nil }
  has_many :nested_licences, :class_name => 'Licence', :dependent => :destroy,
           :autosave => true,
           :foreign_key => :parent_licence_id,
           :finder_sql => proc {
             "SELECT l.* FROM licences l WHERE l.parent_licence_id = #{id}" },
          :counter_sql => proc {
             "SELECT COUNT(*) FROM licences l WHERE l.parent_licence_id = #{id}" }
end

Now I can do this:

test "getting licences on owned dongles" do
  org = organisations(:some_other_corp)
  assert_equal [licences(:licence_4)], org.licences_on_owned_dongles
end

That happily passes. Since it’s an association, you might thing you can find() on it:

test "getting licences on owned dongles and then filtering further" do
  org = organisations(:some_other_corp)
  conditions = { :owner_organisation_id => nil }
  assert_equal [licences(:licence_4)],
    org.licences_on_owned_dongles.find(:all, :conditions => conditions)
end

But this gives:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: dongles.organisation_id: SELECT "licences".* FROM "licences" WHERE "licences"."parent_licence_id" IS NULL AND (("dongles".organisation_id = 72179513)) AND ("licences".parent_licence_id = 747059259)
test/unit/organisation_test.rb:123:in `test_getting_licences_on_owned_dongles_and_then_filtering_further'

In fact, this even occurs when all you call is find(:all). It isn’t just SQLite either, because I noticed this in production (oops) on MySQL.

So I don’t know. It’s really too mysterious to investigate further. I might shelve it as a “Rails just can’t do find() on an association”, use a block to filter it and leave it at that. But I wanted to put it out, just in case there is a better option.

(Actually if you look at the query Rails is generating, it is complete nonsense. Somehow it has ended up generating a query where something has to be NULL and equal to a value at the same time. Even if the query worked, this will return 0 rows.)

  • 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-18T04:06:08+00:00Added an answer on June 18, 2026 at 4:06 am

    Don’t use find in a Rails 3 app.

    org.licences_on_owned_dongles.find(:all, :conditions => conditions)
    

    should be

    org.licences_on_owned_dongles.where(conditions)
    

    Edit: Read up on it here.

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

Sidebar

Related Questions

I have some classes that look like this: MODEL public abstract class BaseEntity<O extends
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
Lets say I have a set of model classes like this: public class Person
Let's say I have some model objects that resemble this: public class FooModel {
I have a class that contains data from some model. This class has metadata
In some Rspec model specs, we have several tests like this: it { should
I have some classes extended this way: class Baseresidence extends CActiveRecord { public static
I have designed some classes using Visual Studio class diagramming. Now I would like
I have some domain classes that look something like this, that I want to
Currently I'm using a base class for some Customer model classes. As defined as

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.