2013-12-18 17 views
1

我想设置一个验证,以便人们不能提交帖子,除非他们点击该帖子的类别,还要确保他们只能选择该帖子的其中一个分类所以帖子只能有一个类别。下面是modles使用验证与has_many_through

class Categorization < ActiveRecord::Base 
    belongs_to :post 
    belongs_to :category 
end 

class Category < ActiveRecord::Base 
    has_many :categorizations 
    has_many :posts, :through => :categorizations 
end 

class Comment < ActiveRecord::Base 
    belongs_to :user 
    validates :content, presence: true, 
        length: { minimum: 5 } 
end 

class Post < ActiveRecord::Base 
    belongs_to :user 
    has_many :comments 

    has_many :categorizations 
    has_many :categories, :through => :categorizations 

    validates :title, :content, presence: true, 
        length: { minimum: 5, maximum: 140 } 
end 

也在这里是我的形式:

<%= form_for @post, :html => {:multipart => true} do |f| %> 
    <% if @post.errors.any? %> 
    <div id="error_explanation" class="animated tada"> 
     <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> 
     <ul> 
     <% @post.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
    <div class="field"> 
    <%= f.label :title %><br> 
    <%= f.text_field :title %> 
    </div> 
    <div class="field"> 
    <%= f.file_field :image %> 
    </div> 
    <div class="field"> 
    <%= f.label :content %><br> 
    <%= f.text_area :content %> 
    </div> 
    <%= hidden_field_tag "post[category_ids][]", nil%> 
    <% Category.all.each do |category| %><br> 
    <%= check_box_tag "post[category_ids][]", category.id, @post.category_ids.include?(category.id), id: dom_id(category)%> 
    <%= label_tag dom_id(category), category.name %> 
    <% end %> 
    <div class="actions"> 
     <%= f.submit %> 
    </div> 
<% end %> 
</div> 

做任何你知道什么,我需要投入,使我的形式带回来一个消息,说需要选择类别或者只能选择一个类别?

回答

0

我们已经做了类似的事情与has_many :through


验证基于连接模式

我们的解决方案是使用accepts_nested_attributes_for,并验证的连接模型。这可以被看作是非常低效的,但很适合我们:

#app/models/message.rb 
Class Message < ActiveRecord::Base 
    validates :title, :body, 
     :presence => { :message => "Needs A Value!" } 
    accepts_nested_attributes_for :message_subscribers, :allow_destroy => true 
end 

#app/models/message_subscriber.rb 
Class MessageSubscriber < ActiveRecord::Base 
    #Validations 
    validates :subscriber_id, 
      :presence => { :message => "Your Message Needs Subscribers!" } 
end 

此,如果您还没有选择任何subscriber_id的返回一个错误


代码

对你而言,我很想这么做:

class Categorization < ActiveRecord::Base 
    belongs_to :post 
    belongs_to :category 

    #Validation 
    validates :subscriber_id, :presence => { :message => "You need to select a category!" } 
end 

class Post < ActiveRecord::Base 
    belongs_to :user 
    has_many :comments 

    has_many :categorizations 
    has_many :categories, :through => :categorizations 

    #Validation 
    validates :title, :content, presence: true, 
        length: { minimum: 5, maximum: 140 } 

end 

我打算为你包括accepts_nested_attributes_for,但我意识到这将是一个不方便的方式来做你已经做的事情。鉴于此,我相信您可能最适合在您的加入模型中执行验证,但如果它不起作用,我们将添加accepts_nested_attributes_for功能