我有一个带有友好ID的自定义slug模型:
# == Schema Information
#
# Table name: blogs
#
# id :integer not null, primary key
# title :string not null
# content :text not null
# is_published :boolean default("false"), not null
# slug :string
# publishing_date :datetime
# tags :text default("{}"), not null, is an Array
# meta_keywords :text default("{}"), not null, is an Array
# meta_description :text
# page_title :string(70)
# author_id :integer default("1")
# blog_category_id :integer default("1")
# author_type :integer default("0"), not null
# created_at :datetime not null
# updated_at :datetime not null
#
class Blog < ActiveRecord::Base
enum author_type: [:content_contributor]
enum publishing_status: [:reviewing, :accepted, :rejected]
include FriendlyId
friendly_id :title_and_category, use: [:slugged, :history]
has_attached_file :featured_image, styles: { large: '600x600>', medium: '300x300>', thumb: '100x100>' }, default_url: 'No_image.jpg.png'
validates_attachment_content_type :featured_image, content_type: /\Aimage\/.*\Z/
belongs_to :author, class_name: 'ContentContributor', foreign_key: 'author_id', inverse_of: :blogs
belongs_to :featured_product, class_name: 'Product', foreign_key: 'featured_product_id', inverse_of: :blogs
belongs_to :blog_category, inverse_of: :blogs
belongs_to :deleted_by, class_name: 'User', foreign_key: 'deleted_by_id'
validates :author, presence: true
validates :author_type, presence: true
validates :title, presence: true
validates :content, presence: true
validates :tags, presence: true
validates :meta_keywords, presence: true
def title_and_category
"#{blog_category.name} #{title}"
end
end
我已经为这样的模型编写了规范:
it { should define_enum_for(:author_type) }
it { should define_enum_for(:publishing_status) }
it { should belong_to(:author).class_name("ContentContributor").with_foreign_key("author_id").inverse_of(:blogs) }
it { should belong_to(:featured_product).class_name("Product").with_foreign_key("featured_product_id").inverse_of(:blogs) }
it { should belong_to(:deleted_by).class_name("User").with_foreign_key("deleted_by_id") }
it { should belong_to(:blog_category).inverse_of(:blogs) }
it { should validate_presence_of(:author) }
it { should validate_presence_of(:author_type) }
it { should validate_presence_of(:title) }
it { should validate_presence_of(:content) }
it { should validate_presence_of(:tags) }
it { should validate_presence_of(:meta_keywords) }
以上错误导致以下错误:
故障:
1) Blog should require author to be set
Failure/Error: it { should validate_presence_of(:author) }
NoMethodError:
undefined method `name' for nil:NilClass
# ./app/models/blog.rb:41:in `title_and_category'
# ./spec/models/blog_spec.rb:33:in `block (2 levels) in <top (required)>'
2) Blog should require author_type to be set
Failure/Error: it { should validate_presence_of(:author_type) }
NoMethodError:
undefined method `name' for nil:NilClass
# ./app/models/blog.rb:41:in `title_and_category'
# ./spec/models/blog_spec.rb:34:in `block (2 levels) in <top (required)>'
3) Blog should require title to be set
Failure/Error: it { should validate_presence_of(:title) }
NoMethodError:
undefined method `name' for nil:NilClass
# ./app/models/blog.rb:41:in `title_and_category'
# ./spec/models/blog_spec.rb:35:in `block (2 levels) in <top (required)>'
4) Blog should require content to be set
Failure/Error: it { should validate_presence_of(:content) }
NoMethodError:
undefined method `name' for nil:NilClass
# ./app/models/blog.rb:41:in `title_and_category'
# ./spec/models/blog_spec.rb:36:in `block (2 levels) in <top (required)>'
5) Blog should require tags to be set
Failure/Error: it { should validate_presence_of(:tags) }
NoMethodError:
undefined method `name' for nil:NilClass
# ./app/models/blog.rb:41:in `title_and_category'
# ./spec/models/blog_spec.rb:37:in `block (2 levels) in <top (required)>'
6) Blog should require meta_keywords to be set
Failure/Error: it { should validate_presence_of(:meta_keywords) }
NoMethodError:
undefined method `name' for nil:NilClass
# ./app/models/blog.rb:41:in `title_and_category'
# ./spec/models/blog_spec.rb:38:in `block (2 levels) in <top (required)>'
我删除了自定义名称构建的代码,并且所有上述内容都已通过,但未使用自定义slug构建。应该改变什么?
答案 0 :(得分:2)
这是因为FriendlyId
使用before_validation callback
设置了一个slug。在您在类中定义的任何before_validation回调之前调用此方法。
在您的其他验证之前调用title_and_category
方法。
要解决这个问题,你可以在设置before_validation回调后调用friendly_id,或者你可以在friendly_id之前添加你的方法:
假设您有一个title_and_category
列而不是方法。
# Make sure to prepend it so that it runs before Friendly_id's own callback
# http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
before_validation :set_title_and_category, prepend: true
def set_title_and_category
self.title_and_category = "#{blog_category.name} #{title}"
end
def title_and_category
"#{blog_category.name} #{title}" if blog_category
end
我发现您使用的是shoulda-matchers
,您可以使用shoulda callback matchers并在测试中添加此代码
it { should callback(:generate_slug).before(:validation) }