English 中文(简体)
我如何在两个没有遵循任何命名公约的领域之间建立联系?
原标题:how do I build an association between 2 fields that do not follow any naming convention?

我的公司有一个老网站,这个网站是多年前在PHP建造的。 我认为,数据库图示是从战略角度考虑的要求的缓慢积累。 现在,我们正在推出PHP代码,并利用铁路建造该地。 数据库图形仍保持不变。

我只是把我的头 head放在铁路上,因此这或许是一个简单的问题。

我有一份数据看似如此的文章:

id: 1102129
article_type_id: 5
old_article_id: 0
title: Zombie Film Locations
subtitle: Hollywood-inspired survival tips for destinations teeming with the undead
display_date: 0000-00-00
author_id: 29
thumbnail_image_id: 12033473
index_image_id: 12033473
article_image_id: 12033473
is_archived: 0
is_featured: 1
is_published: 1
date_to_show: 2011-10-04 00:00:00
off_the_path: 0
created: 2011-10-04 12:45:07
modified: 2011-10-11 15:33:59
fact_check_date: 0000-00-00 00:00:00
unpublish_date: 0000-00-00
is_qsfeatured: 1
featured_order: 0

第二行:

id: 1102128
article_type_id: 14
old_article_id: 0
title: Hotel Adagio [id]35677
subtitle: Hotel Adagio
display_date: 2011-09-29
author_id: 0
thumbnail_image_id: NULL
index_image_id: NULL
article_image_id: NULL
is_archived: 0
is_featured: 0
is_published: 1
date_to_show: 2011-09-29 12:50:38
off_the_path: 0
created: 2011-09-29 12:50:38
modified: 2011-09-29 12:50:38
fact_check_date: NULL
unpublish_date: NULL
is_qsfeatured: 0
featured_order: NULL

图像数据如下:

id: 6722
old_id: 0
type: Index
association_type: TopTen
association_id: 1102129
position: 0
copyright: AMC
caption: The Walking Dead portrays an abandoned, corpse-ridden downtown Atlanta, one of 10 zombie film locations we ve scouted out to help you survive (and hide) should the dead rise.
alt_text: Zombie film locations
url: photos/index-TopTens/walkingdeadindex.jpg
_url: 
name: index
published: 0
description: 

第二行:

id: 6723
old_id: 0
type: Thumbnail
association_type: TopTen
association_id: 1102129
position: 0
copyright: AMC
caption: The Walking Dead portrays an abandoned, corpse-ridden downtown Atlanta, one of 10 zombie film locations we ve scouted out to help you survive (and hide) should the dead rise.
alt_text: Zombie film locations
url: photos/thumb-TopTens/walkingdeadthumb.jpg
_url: 
name: thumbnail
published: 0
description:

图像表中的协会是该文章的背书,尽管数据库中没有外国钥匙。

在我获得这篇文章时,我如何掌握所有图像?

我现在担任控制者:

@articles = Article.where(:article_type_id => 5).order("id DESC").limit(5)

在我看来,我确实像:

<%= article.title %>

But how can I reference the images, from the article model?

To make this more complicated, the images are polymorphic, so I can not simply create a foreign key from images to articles. Images also sometimes get mapped to "travel_guides" and "slideshows", which are 2 other tables we have.

最新资料:

改变 该条现在规定:

has_many :images, :foreign_key => association_id

修改我的控制器法,以便:

@articles = Article.where(:article_type_id => 5).order("id DESC").limit(5).joins(:images)

但我认为我确实:

b) ;

我发现的是“无方法”错误。

我正在这样做:

b) ;

没有图像上的“s”,然后我去“无方法图像”。

最新资料:

因此,在指挥线上,我跑了“铁路”进入奥塞莱,然后我用我的控制器操作Im的手法:

@articles = Article. where (article_type_id) => 5).order (“id DESC”).limit(5).joins (:image)

请允许我:

 @articles = Article. where (article_type_id) => 5).order (“id DESC”).limit(5).joins (:image)
  Article Load (11.5ms)  SELECT `articles`.* FROM `articles` INNER JOIN `images` ON `images`.`association_id` = `articles`.`id` WHERE `articles`.`article_type_id` = 5 ORDER BY id DESC LIMIT 5
 => [#<Article id: 1102129, article_type_id: 5, year_article_id: 0, name: “Zombie 电影 Sites”, sub entitled: “Hollywood-inspired life tips forapas t.pubglulature_com_date:nil, author_id: 29, thumbnail_image_id: 12033473, article_image_id: 12033473, article_image_id: 12033473, is_archived: false, is_featured: realent, ispublished: real,date:date_en, pmto_#<Article id: 1102129, article_type_id: 5, year_article_id: 0, name: “Zombie 电影 Sites”, sub entitled: “Hollywood-inspired life tips forapas t.pubglulature_com_date:nil, author_id: 29, thumbnail_image_id: 12033473, article_image_id: 12033473, article_image_id: 12033473, is_archived: false, is_featured: realent, ispublished: real,date:date_en, pmto_#<Article id: 1102129, article_type_id: 5, year_article_id: 0, name: “Zombie 电影 Sites”, sub entitled: “Hollywood-inspired life tips forapas t.pubglulature_com_date:nil, author_id: 29, thumbnail_image_id: 12033473, article_image_id: 12033473, article_image_id: 12033473, is_archived: false, is_featured: realent, ispublished: real,date:date_en, pmto_#<Article id: 1102122, article_type_id: 5, old_article_id: 0, title: "Nude Vacations", subtitle: "These places to get naked around the world make for...", display_date: nil, author_id: 574, thumbnail_image_id: 12024629, index_image_id: 12024629, article_image_id: 12024629, is_archived: false, is_featured: true, is_published: true, date_to_show: "2011-09-23 00:00:00", off_the_path: false, created: "2011-09-23 13:13:41", modified: "2011-10-18 10:44:15", fact_check_date: nil, unpublish_date: nil, is_qsfeatured: true, featured_order: 2, slug: "nude-vacations">, #<Article id: 1102122, article_type_id: 5, old_article_id: 0, title: "Nude Vacations", subtitle: "These places to get naked around the world make for...", display_date: nil, author_id: 574, thumbnail_image_id: 12024629, index_image_id: 12024629, article_image_id: 12024629, is_archived: false, is_featured: true, is_published: true, date_to_show: "2011-09-23 00:00:00", off_the_path: false, created: "2011-09-23 13:13:41", modified: "2011-10-18 10:44:15", fact_check_date: nil, unpublish_date: nil, is_qsfeatured: true, featured_order: 2, slug: "nude-vacations">] 

因此,尽管如此,图像数据没有显示。

最新资料:

Huh,现在我看到,在这一街区,我getting在铁路上,我看到每条都在退回3次。

Article id: 1102129, article_type_id: 5, year_article_id: 0, name: “Zombie 电影 Sites”, sub entitled: “Hollywood-inspired life tips forapas t.pubglulature_com_date:nil, author_id: 29, thumbnail_image_id: 12033473, article_image_id: 12033473, article_image_id: 12033473, is_archived: false, is_featured: realent, ispublished: real,date:date_en, pmto_

,

,

因此,由于有3个图像协会,我收到3篇文章,但我没有图像。 这是两个世界中最糟糕的情况。

对图像进行单独查询比较容易。

最新资料:

这给我带来了我希望的东西,但我看不出,我似乎无法把这变成一个铁路点:

SELECT articles.title, articles.subtitle, images.url, images.caption 
FROM articles 
JOIN images ON articles.id = images.association_id 
WHERE images.type= Thumbnail  
ORDER BY articles.id desc 
LIMIT 5 G

最新资料:

我未能做到这一点。 我认为,一名鲁比辅导员教授我一些基本的鲁比和铁路。 我很高兴付出代价。 我在此重述我的问题:

问题回答

查阅某篇文章的图像,需要添加一个艺术类的协会:

class Article
  has_many :images, :foreign_key => :association_id
end

您提到,有时图像被绘制成图,以前往_和幻灯(2个新增表格)。 你必须增加每个桌子的联系。 因此,你可以做这样的事情:

class Article < ActiveRecord::Base
  has_many :images, :foreign_key => :association_id
  has_many :travel_guides, :foreign_key => :association_id
  has_many :slideshow_images, :foreign_key => :association_id
end

成立反向协会通常也是一个好的想法:

class Image < ActiveRecord::Base
  belongs_to :article, :foreign_key => :association_id
end
class TravelGuide < ActiveRecord::Base
  belongs_to :article, :foreign_key => :association_id
end
class SlideshowImage < ActiveRecord::Base
  set_table_name :slideshows
  belongs_to :article, :foreign_key => :association_id
end

考虑的一些事项:

  • 我离开了:*has_many*协会的独立选择。 这通常不是一个好的想法。 您可以设立:depedent => :destroy,以便你能够把一些清理逻辑放在你的形象班上(删除图像档案)。

  • 如果你没有简单的加入,那么你应考虑增加“:conditions,选择“s_many”(或归属)。

  • 如果上述建议没有解决你的所有问题,那么你可以使用的唯一表格继承和多用途协会。

当你在图像协会中指明“谁是谁”时,你就说一篇文章有图像,这样一条。 图像回归一阵列。 在阵列上没有界定方法。 为了使用尿液方法,你需要接触每个阵列成员,并将ur子称作该物体。 类似:

article.images[0].url

article.images.first.url

你们需要在模式中具体说明这一联系。 有一个像你这样精彩的公约,但你所表完全照此,但根本不是问题。 有两个可能的解决办法。

1) 您更名为数据库中的一栏(当仪器仍在生产中时,我们就赢得了工作)。

b) 请具体说明使用以下关键参数的协会:

条款:

has_many :images, :foreign_key =>  association_id 

照片:

belongs_to :article, :foreign_key =>  association_id 

但有些事情需要你参加。 你们会遇到一些问题,把你的桌上形象命名,因为还有一张公共圆顶“影像”,当你把照片从一些铁路把它解释为一种id,试图从数据库中挑选一个图像,而不是提供图像。

Secound 问题可能在于,当事人是一个称为“联系-类型”的田地,表明变异者协会。 当你转向铁路时,NYOu可能更名声。

articles = Article.includes(:images).where(:article_type_id => 5, :images => { :image_type =>  Thumbnail  }).order( articles.id DESC ).limit(5) #no database query is fired yet by rails
article = articles.first # Here we fire the first query, and this query also fetches the image of the article 
article.images.first #again fires no query but returns the first image of the article

There is another case where you should fire db query to fetch only one image from the database. Then you should do something like,

articles = Article.where(:article_type_id => 5).order("id DESC").limit(5) #again fires no db query
article = articles.first #fires db query to get first article
image = article.images.first #fires query for only first image of the article 

第一种方法是有效的,如果每条图像数量较少,但第二种方法是有效的,如果每条有大量图像的话。

现在,你对上面标出的钢 the的铁道,

SELECT articles.title, articles.subtitle, images.url, images.caption 
FROM articles 
JOIN images ON articles.id = images.association_id 
WHERE images.type= Thumbnail  
ORDER BY articles.id desc 
LIMIT 5
articles = Article.joins(:images).select("articles.title, articles.subtitle, images.url, images.caption").where("images.type =  Thumbnail ").order("articles.id DESC").limit(5)
article = articles.first 
first_image = article.images.first




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签