我有一个文章系统,其中的文章可以位于不同的部分,例如食谱、指南、社论、新闻等。
这些文章中的每一篇也可以属于一个或多个类别。例如,食谱可以属于松饼类别和蓝莓类别。
然后当我生成 url 时,它的格式为:
example.com/{post_section}/{primary_post_category_slug}/{post_title}/
在列出文章时,我会在标题上方显示文章所属的每个类别。这些类别中的每一个都是一个链接,单击该链接时仅显示属于该类别的文章。
我的问题是,当列出一个类别中的所有文章时,如果一篇文章有多个类别,我不知道如何检索主要类别。例如,如果我有一篇关于蓝莓松饼食谱的文章,我会把它放在松饼的主要类别和蓝莓的次要类别中。
现在,如果访问者浏览蓝莓类别,它应该列出所有与蓝莓相关的文章,但链接仍应包含主要类别 slug 是什么。我的问题是检索与我们列出的类别不同的主要类别。
下面是我正在使用的示例查询和表结构:
select p.*, c.*, t.* from posts p
left join post_category_selected a on a.post_id = p.post_id
left join post_category c using (post_category_id)
left join post_section t using (post_section_id)
and c.post_category_slug = "blueberry"
and t.post_section_slug = "recipes"
order by p.post_publish_date desc
这确实会返回所有具有蓝莓类别的文章,但我不知道如何检索松饼的 primary_category,因此我可以在生成链接时使用它。
希望这是有道理的。
表结构:
posts
|- post_id
|- post_title
|- post_section
|- post_slug
|- post_publish_date
post_category
|- post_category_id
|- post_section_id
|- post_category
|- post_category_slug
post_category_selected
|- post_category_selected_id
|- post_id
|- post_category_id
|- is_primary (enum Y,N)
post_section
|- post_section_id
|- post_section_title
|- post_section_slug
最佳答案
您需要分别加入 post_category 和 post_category_selected 两次,一次用于所选类别,一次用于主要类别。像这样:
select p.*, c1.*, t.*,
c2.post_category_slug as primary_category_slug
from posts p
left join post_category_selected a1 on a1.post_id = p.post_id
left join post_category c1 using (post_category_id)
left join post_section t using (post_section_id)
and c.post_category_slug = "blueberry"
and t.post_section_slug = "recipes"
left join post_category_selected a2 on a2.post_id = p.post_id
and a2.is_primary = 'Y'
left join post_category c2 on c2.post_category_id = a2.post_category_id
order by p.post_publish_date desc
您可能需要从 c2 添加一些额外的列,具体取决于您的其他要求。
关于MySQL 查询按特定类别列出多类别文章,同时仍检索主猫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34910127/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
在读取/解析文件(使用Ruby)时忽略某些行的最佳方法是什么?我正在尝试仅解析Cucumber.feature文件中的场景,并希望跳过不以Scenario/Given/When/Then/And/But开头的行。下面的代码有效,但它很荒谬,所以我正在寻找一个聪明的解决方案:)File.open(file).each_linedo|line|line.chomp!nextifline.empty?nextifline.include?"#"nextifline.include?"Feature"nextifline.include?"Inorder"nextifline.include?
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
我正在我的Rails项目中安装Grape以构建RESTfulAPI。现在一些端点的操作需要身份验证,而另一些则不需要身份验证。例如,我有users端点,看起来像这样:moduleBackendmoduleV1classUsers现在如您所见,除了password/forget之外的所有操作都需要用户登录/验证。创建一个新的端点也没有意义,比如passwords并且只是删除password/forget从逻辑上讲,这个端点应该与用户资源。问题是Grapebefore过滤器没有像except,only这样的选项,我可以在其中说对某些操作应用过滤器。您通常如何干净利落地处理这种情况?
我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token
我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin
我在Rails上使用带有ruby的solr。一切正常,我只需要知道是否有任何现有代码来清理用户输入,比如以?开头的查询。或* 最佳答案 我不知道执行此操作的任何代码,但理论上可以通过查看parsingcodeinLucene来完成并搜索thrownewParseException(只有16个匹配!)。在实践中,我认为您最好只捕获代码中的任何solr异常并显示“无效查询”消息或类似信息。编辑:这里有几个“sanitizer”:http://pivotallabs.com/users/zach/blog/articles/937-s
我正在为锦标赛开发一个Rails应用程序。我在这个查询中使用了三个模型:classPlayertruehas_and_belongs_to_many:tournamentsclassTournament:destroyclassPlayerMatch"Player",:foreign_key=>"player_one"belongs_to:player_two,:class_name=>"Player",:foreign_key=>"player_two"在tournaments_controller的显示操作中,我调用以下查询:Tournament.where(:id=>params