English 中文(简体)
铁路加入表问题
原标题:rails join table problem
  • 时间:2009-09-18 11:25:17
  •  标签:

Part of my RoR application is responsible for managing web site designs portfolio. One web site design can have many images associated with it. One image can be associated only with one design. I use has_many statement with :through parameter to connect images with design through join table. And when image is deleted associated entry in join table must be deleted. So i have next models For images:

class  Image < ActiveRecord::Base
  has_one :images_site_designs , :class_name => "ImagesSiteDesigns" , :dependent => :destroy
  has_one :site_design , :through => :images_site_designs
end

现场设计:

class SiteDesign < ActiveRecord::Base
  belongs_to :client
  has_many :images_site_designs , :class_name => "ImagesSiteDesigns"
  has_many :images , :through => :images_site_designs
end

加入图表:现场设计:

class ImagesSiteDesigns < ActiveRecord::Base
  belongs_to :image 
  belongs_to :site_design
end

• 为网站设计制作新的图像,是ok,因此,光线编码可操作:

   @site_design = SiteDesign.find(params[:id])
   @site_design.images << Image.new(params[:image])

但在试图删除下个错误时,似乎有:

 ActiveRecord::StatementInvalid in ImagesController#destroy

Mysql::Error: Unknown column  id  in  where clause : DELETE FROM `images_site_designs` WHERE `id` = NULL

看来,铁路在盘问图像时使用错误的栏目名称——现场设计公司加入表。 如何加以确定?

概述:

图像-控制器功能,删除图像:

  def destroy
    @image = Image.find(params[:id])
    @image.destroy

    respond_to do |format|
      format.html { redirect_to(images_url) }
      format.xml  { head :ok }
    end
  end

移民:

class CreateImages < ActiveRecord::Migration
  def self.up
    create_table :images do |t|
      t.string :url
      t.string :name
      t.text :description

      t.timestamps
    end
  end

  def self.down
    drop_table :images
  end
end
class CreateSiteDesigns < ActiveRecord::Migration
  def self.up
    create_table :site_designs do |t|
      t.string :name
      t.text :concept
      t.text :description
      t.integer :client_id

      t.timestamps
    end
  end

  def self.down
    drop_table :site_designs
  end
end

class CreateImagesSiteDesigns < ActiveRecord::Migration
  def self.up
    create_table :images_site_designs , :id => false do |t|
      t.integer :image_id
      t.integer :site_design_id
    end
  end

  def self.down
    drop_table :images_site_designs
  end
end
最佳回答

不能真正回答你的问题,因为没有显示什么法典会引发错误。 或许可以呼吁在图像上销毁,但可以肯定。

然而,它并不像你一样需要加入任何途径。 满足这一要求的似乎是:

class SiteDesign < ActiveRecord::Base
  belongs_to :client
  has_many :images
end

class Image < ActiveRecord::Base
  belongs_to :site_design
end

当然,这将需要移民(跳跃加入桌旁,增加网站——设计——帮助图像表),但似乎是一种更清洁的解决办法。 为什么不这样做?

问题回答

这里的问题是,你有移民,使你的形象——即设计的NOT成为一种模式(无背一栏),但你当时正在为此树立一种积极的记录模式。 为了让你做些什么工作,你实际上需要在你加入的表格中打一栏。

如果你想要一个图像只能与一个设计相联系,那么你为什么会使用一个老板——和——信人——关系(你用一个桌子!)

我将支持以下工作:

(1) 降低你的图像模式,增加一个网站——设计_id属性

class CreateImages < ActiveRecord::Migration
  def self.up
    create_table :images do |t|
      t.string :url
      t.string :name
      t.text :description
      t.integer :site_design_id

      t.timestamps
    end
  end

  def self.down
    drop_table :images
  end
end

2) DropsSiteDesigns-Migration

drop_table :images_site_designs

3) 将模型改为:

class SiteDesign < ActiveRecord::Base
  belongs_to :client
  has_many :images
end

class  Image < ActiveRecord::Base
  belongs_to :site_design
end

因此,你以1:n关系告终,这是你具体阐述的最佳解决办法。

您的榜样如下:

Image.first.site_design
=> <SiteDesign #id:...>
SiteDesign.first.image
=> <Image #id...>

SiteDesign.image = Image.new(params[:image])
...




相关问题
热门标签