#46 ✓resolved
Rob

Uninitialized constant using polymorphic association

Reported by Rob | January 21st, 2009 @ 10:14 AM

Using a polymorphic association, I get an "uninitialized constant" error when I click on an order_by_link. For a test case, I set up the following example, where the regular association "post" works as expected, while the polymorphic association "resource" returns "uninitialized constant CatalogEntry::Resource" when clicking on their respective links. Using version 1.6.2.

I am a Rails noobie, my apologies if I'm missing something obvious, and thanks for a very useful plugin.


class CatalogEntry < ActiveRecord::Base
  belongs_to :resource, :polymorphic => true
  belongs_to :post
end

class Article < ActiveRecord::Base
  has_one :catalog_entry, :as => :resource
end

class Post < ActiveRecord::Base
  has_one :catalog_entry
end

create_table :catalog_entries
  t.string :name
  t.integer :resource_id
  t.string :resource_type
  t.integer :post_id
end

create_table :articles
  t.text :content
end

create_table :posts
  t.text :content
end

class Jquery::CatalogEntriesController < ApplicationController
  def index
    @search = CatalogEntry.new_search(params[:search])
    @cats, @cats_count = @search.all, @search.count
  end
end

<tr>
    <th><%= order_by_link :id %></th>
    <th><%= order_by_link :resource => :content %></th>
    <th><%= order_by_link :post => :content %></th>
</tr>
<% @cats.each do |cat| %>
<tr>
  <td><%= cat.id %></td>
  <td><%= cat.resource.content%></td>
  <td><%= cat.foo.content%></td>
</tr>

Comments and changes to this ticket

  • Rob

    Rob January 27th, 2009 @ 08:59 AM

    Is there any other information you'd like me to provide?

    Thanks.

  • Ben Johnson

    Ben Johnson January 27th, 2009 @ 02:30 PM

    • State changed from “new” to “open”

    You can't order by a polymorphic relationship because I'm not dealing with objects, I'm on the class level trying to get a collection of objects. That being said I don't know what tables to join, etc. Can you give me an example of what you are trying to do without searchlogic? How would you do this in the console without searchlogic?

  • Rob

    Rob January 29th, 2009 @ 10:19 AM

    Thanks for the explanation. I was able to solve my problem by restructuring my database.

  • Ben Johnson

    Ben Johnson January 29th, 2009 @ 12:06 PM

    • State changed from “open” to “resolved”
  • Rich

    Rich March 25th, 2009 @ 03:30 PM

    @ben, I have the same issue. I understand why polymorphic assns aren't working in a condition, i.e.:

    class Comment < ActiveRecord::Base belongs_to :subject, :polymorphic => true end

    s = Comment.new_search s.conditions.subject.id = 123 # causes Uninitialized constant error

    Do you have a work around for this? If you're working on the class level, is there anyway to detect a polymorphic assn then resolve the appropriate table name?

  • Elaine730

    Elaine730 January 30th, 2019 @ 02:16 AM

    Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep it up. kodi

  • sandy

    sandy May 12th, 2019 @ 12:20 PM

    window 10 is one of the most used operating system in the world.if you want to use other PC and It has password protected. you can visit here

    https://removepasswordwindows10.xyz/ and learn how to remove password in windows 10

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Provides common named scopes and object based searching.

People watching this ticket

Pages