【发布时间】:2011-04-24 05:08:46
【问题描述】:
我正在使用has_many_polymorphs 插件,以便可以将视频、主题和用户发布到个人资料中。因此,个人资料有许多“showable_objects”,可以是视频、主题和用户。此外,创建“showable_object”的用户也将与其关联。我已经设置了模型关联(如下)。
我希望创建 showable_object 的方式是让用户从资源显示页面上的自动完成字段中选择另一个用户。然后将该资源与所选用户的个人资料相关联。
我的问题是我应该如何设置我的 showable_objects 控制器?另外,如果我希望它通过 AJAX 发送,AJAX jQuery 请求会是什么样子?
更新:
这是我的模型关联:
class ShowableObject < ActiveRecord::Base
belongs_to :showable_object, :polymorphic => true
belongs_to :user
belongs_to :profile
end
class Profile < ActiveRecord::Base
has_many_polymorphs :showable_objects, :from => [:videos, :users, :topics, :video_votes],
:dependent => :destroy
end
class User < ActiveRecord::Base
has_many_polymorphs :showable_objects, :from => [:videos, :users, :topics, :video_votes],
:dependent => :destroy
end
这是我的 ShowableObject 迁移:
class CreateShowableObjects < ActiveRecord::Migration
def self.up
create_table :showable_objects do |t|
t.references :showable_object, :polymorphic => true
t.references :profile
t.references :user
t.timestamps
end
end
def self.down
drop_table :showable_objects
end
end
顺便说一句,我也从这些关联中得到了这个错误(所以这实际上是一个两部分的问题:P):
ActiveRecord::Associations::PolymorphicError in Videos#index
Showing /rubyprograms/dreamstill/app/views/videos/_video.html.erb where line #24 raised:
Could not find a valid class for :showable_objects_users (tried ShowableObjectsUser). If it's namespaced, be sure to specify it as :"module/showable_objects_users" instead.
它指向这条线 <% if video.owned_by? current_user %> 并且与用户模型中的 has_many_polymorphs 调用有关。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 crud has-many-polymorphs