使用has_many通过验证

时间:2013-12-18 08:59:34

标签: ruby-on-rails ruby-on-rails-4

我想设置验证,以便人们无法提交帖子,除非他们点击该帖子的类别,并确保他们只能选择该帖子的其中一个类别,以便帖子可以有只有一个类别。这是模型

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>

你们是否知道我需要投入什么才能让我的表格回来时带有一条消息,说明需要选择类别或只能选择一个类别?

1 个答案:

答案 0 :(得分: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功能