
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
-
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?
-
Ben Johnson January 29th, 2009 @ 12:06 PM
- State changed from open to resolved
-
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?
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.
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.