小编zec*_*htz的帖子

Rails与表单collection_select的多态关联,没有嵌套

我的应用程序中有以下模型

class User < ActiveRecord::Base
has_many :articles
has_many :tour_companies
has_many :accomodations
end

class Articles < ActiveRecord::Base
belongs_to :user
belongs_to :bloggable, :polymorphic => true
end

class TourCompany < ActiveRecord::Base
belongs_to :user
has_many :articles, :as => :bloggable
end

class Accommodation < ActiveRecord::Base
belongs_to :user
has_many :articles, :as => :bloggable
end
Run Code Online (Sandbox Code Playgroud)

现在我的问题是我想要一个登录用户能够写一篇文章并使用表单collection_select来选择他/她的旅游公司或文章应该与之相关的住宿,我如何在rails 4中做到这一点?如何从表单集选择中选择bloggable类型和id?我不想要嵌套资源

polymorphic-associations ruby-on-rails-4

4
推荐指数
2
解决办法
2300
查看次数