我正在尝试保存具有类别关系的帖子。 这些是我的模特
class Post < ActiveRecord::Base
has_many :categorizes
has_many :post_categories, :through=>:categorizes
accepts_nested_attributes_for :post_categories
end
class PostCategory < ActiveRecord::Base
has_many :categorizes
has_many :posts, :through=>:categorizes
end
class Categorize < ActiveRecord::Base
belongs_to :post
belongs_to :post_category
end
并在ActiveAdmin post.rb中。
ActiveAdmin.register Post do
permit_params :title, :content, post_category_ids:[:id]
index do
selectable_column
id_column
column :title
column :post_category_id
column :created_at
actions
end
filter :created_at
form do |f|
f.inputs "Post Details" do
f.input :title
f.input :content,:input_html => { :class => "tinymce_editor" }
#f.input :post_categories, :as=> :check_boxes#, :collection => PostCategory.all
end
f.has_many :post_categories ,new_record: false do |c|
c.inputs do
c.input :title
end
end
f.actions
end
controller do
defaults :finder => :find_by_slug_url
end
end
我需要从post_categories查看我的所有类别,我应该选择多个。 我检查rails控制台,但帖子没有任何类别。
Post.First.post_categories equal to []