我有3个模型,都使用了宝石 - https://github.com/digitalplaywright/mongoid-slug - 从模型标题字段创建一个_slugs字段。
我有一篇文章,我需要找到它所属的问题。我尝试过很多东西,但似乎没什么用。
关于使用我的文章slug获取属于文章的问题的正确查询的任何建议?
查询不起作用:
p = Publication.find("my-publication")
p.issues.where(:'articles._slugs'.in => ["an-article-slug"]).first
发布模式:
class Publication
# 1. Include mongoid stuff
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Slug
# 2. Define fields
field :title, type: String
field :description, type: String
field :published, type: Boolean, default: false
field :live, type: Boolean, default: false
field :show_walkthrough, type: Boolean, default: true
field :subscription_duration, type: String, default: "Subscription Duration"
field :subscription_price, type: String, default: "Price"
field :sell_issues_separately, type: String, default: "Individual Issue Sale"
field :issue_price, type: String, default: "Price"
field :previewed_on_device, type: Boolean, default: false
field :shareable, type: String, default: "Make Articles Shareable Online"
field :urban_airship_key, type: String
field :urban_airship_secret, type: String
field :urban_airship_master_secret, type: String
# 3. Set attributes accesible
attr_accessible :title, :description, :live, :published, :show_walkthrough, :subscription_duration, :subscription_price, :sell_issues_separately, :issue_price, :cover_image_attributes, :logo_image_attributes, :shareable, :urban_airship_key, :urban_airship_secret, :urban_airship_master_secret
# 4. Set slug
slug :title, reserve: ['new', 'edit', 'walkthrough', 'email', 'previewer', 'privacy', 'support', 'manifest', 'feed', 'demo', 'existence', 'switch']
# 5. Set associations
belongs_to :user
embeds_many :issues, order: :created_at.desc, cascade_callbacks: true
end
问题模型:
class Issue
# 1. Include mongoid stuff
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Slug
# 2. Define fields
field :title, type: String
field :description, type: String
field :published, type: Boolean, default: false
field :last_push_at, type: DateTime, default: Time.now
field :published_at, type: DateTime, default: Time.now
field :no, type: Integer, default: 0
field :color, type: String
field :free, type: Boolean, default: false
# 3. Set attributes accesible
attr_accessible :title, :description, :published, :last_push_at, :published_at, :no, :color, :free, :cover_image_attributes
# 4. Set slug
slug :title, scope: :publication, reserve: ['new', 'edit', 'publish', 'update_order']
# 5. Set associations
embedded_in :publication
embeds_many :articles, :as => :articleable, :class_name => 'Article', cascade_callbacks: true, order: :no.desc
embeds_one :cover_image, :as => :imageable, :class_name => 'Image', cascade_callbacks: true, autobuild: true
end
文章模型:
class Article
# 1. Include mongoid stuff
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Slug
# 2. Define fields
field :title, type: String
field :author, type: String
field :lead, type: String
field :body, type: String
field :no, type: Integer
# 3. Set attributes accesible
attr_accessible :title, :author, :lead, :body, :no, :article_image_attributes, :article_images_attributes, :article_images_attributes
# 4. Set slug
slug :title, scope: :articleable
# 5. Set associations
embedded_in :articleable, polymorphic: true
embeds_one :article_image, :as => :imageable, :class_name => 'Image', cascade_callbacks: true, autobuild: true
embeds_many :article_images, :as => :imageable, :class_name => 'Image', cascade_callbacks: true
end
更新了新的查询和结果
我可以使用以下@mu的查询建议获取该出版物:
我无法使用相同的查询来解决问题:
如果我在出版物的第一期刊登第一篇文章,你可以在这里看到_slugs字段:
我在这里做错了什么?抓取出版物时,查询似乎很有效。为什么在抓住问题时它不能很好地工作?