如何为连接表创建模型?

时间:2015-07-22 20:04:08

标签: ruby-on-rails sql-server sql-server-2005

我有这个从SQL Server 2005获取数据的查询。

SELECT DocumentNbr AS [Whole Nbr], DocumentType AS [Type], 
        DrawingSize AS [Size], DocumentRevision AS [Revision],
        OtherSheetNbr AS [Other Nbr], SheetNbr AS [Sheet Nbr], 
        NbrOfSheets AS [Of Sheets], FrameNbr AS [Frame Nbr], 
        NbrOfFrames AS [Of Frames], DocumentTitle AS [Doc Title], 
        VolumeLabel AS [Volume], CASE WHEN VolumeNote IS NULL THEN '' ELSE ' * ' END AS [Note], 
        CASE WHEN Blueprint = 1 THEN 'Yes' ELSE 'No' END AS Prnt, CASE WHEN ObsoleteCards = 1 THEN 'Yes' ELSE 'No' END AS Obs, 
        CASE WHEN AcquisitionDate IS NULL THEN tblImage.CreationDate ELSE AcquisitionDate END AS [Acquire Date],
        Mnemonic AS [Source], FilePath, FileName, FileExtension, 
        CASE WHEN IsBaseDoc = 1 THEN 'Yes' ELSE 'No' END AS [Base Doc], 
        AccDocNbr AS [Acc Doc Nbr], CommonSubDirectory, 
        ImageID, tblImage.VolumeID, tblImage.VolumeTypeID, tblVolume.VolumeSourceID, LatestImage 
    FROM ((tblVolume INNER JOIN tblVolumeSource ON tblVolume.VolumeSourceID = tblVolumeSource.VolumeSourceID) 
        INNER JOIN tblVolumeType ON tblVolume.VolumeTypeID = tblVolumeType.VolumeTypeID) 
        INNER JOIN tblImage ON tblImage.VolumeID = tblVolume.VolumeID 
    WHERE DocumentNbr = 'ss9220'

哪一行会返回多行。我找不到有关所有方法的文档(或连接多个表的最佳方法)。我可以使用all方法返回多行传递DocumentNbr?

我是否为每个表制作模型并在单独的模型中使用belongs_to或has_one?

我可以拥有不会映射到表格的模型吗?

处理别名的最佳方法是什么?以上查询来自Access表单。我正在将Access表单转换为rails应用程序。这是我的第一个rails应用程序,我很难学习'rails'方式。我已经完成了3个教程,但它们都是基础的,并没有反映我必须处理的现实世界问题。

数据库架构:

CREATE TABLE [dbo].[tblImage](
    [ImageID] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_Image_ImageID]  DEFAULT (newid()),
    [VolumeID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_tblImage_VolumeID]  DEFAULT (newid()),
    [VolumeTypeID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_tblImage_VolumeTypeID]  DEFAULT (newid()),
    [DocumentNbr] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [CAGE] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [DocumentType] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [DrawingSize] [varchar](5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [DocumentRevision] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [RevisionDate] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,    
    [DocumentTitle] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [OtherSheetNbr] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [SheetNbr] [numeric](10, 0) NULL,
    [NbrOfSheets] [numeric](10, 0) NULL,
    [SheetRevision] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [FrameNbr] [numeric](5, 0) NULL,
    [NbrOfFrames] [numeric](5, 0) NULL,
    [FileType] [int] NULL,
    [FileTypeFormat] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [SourceFlavor] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [DestinationFlavor] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [FileTypeContent] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [FileTypeVersion] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [DFISSourceCAGE] [varchar](12) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [FileName] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [FileExtension] [varchar](6) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [FilePath] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [MediaVolumeID] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [MajorGroup] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [MinorGroup] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [SecurityLevel] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Rights] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ForeignSecure] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_Image_Foreign/Secure]  DEFAULT (0),
    [Nuclear] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_Image_Nuclear]  DEFAULT (0),
    [SubSafe] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_Image_SubSafe]  DEFAULT (0),
    [AirType] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [APL] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [CADInfo] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ControlCode] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HSC] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [NSN] [varchar](40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [UIC] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [System] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Nomenclature] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ShipClass] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ShipTypeHullNbr] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [MasterLocation] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [OfflineLocation] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ParentCAGE] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ParentDocNbr] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [PartNbr] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [SubSheet] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [SucceedingDrwg] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Distribution] [varchar](2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [AccDocKind] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [AccDocNbr] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [AccDocCAGE] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [AccDocRev] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [AccDocSheet] [numeric](18, 0) NULL,
    [AccDocFrame] [numeric](18, 0) NULL,
    [WeaponSystem] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [DFISVersion] [numeric](18, 0) NULL,
    [AltDFISVersion] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [IsBaseDoc] [bit] NULL CONSTRAINT [DF_Image_IsBaseDoc]  DEFAULT (1),
    [HasAccDoc] [bit] NULL CONSTRAINT [DF_Image_HasAccDoc]  DEFAULT (0),
    [CDFileName] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [CDFilePath] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [CreationDate] [datetime] NULL,
    [LatestImage] [bit] NOT NULL CONSTRAINT [DF_tblImage_LatestImage]  DEFAULT (0),
    [ReadAccessCounter] [int] NOT NULL CONSTRAINT     DF_tblImage_ReadAccessCounter]  DEFAULT (0),
 CONSTRAINT [PK_Image] PRIMARY KEY CLUSTERED  (     [ImageID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]

CREATE TABLE [dbo].[tblVolume](
    [VolumeID] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_Volume_VolumeID]  DEFAULT (newid()),
    [VolumeLabel] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [VolumeTypeID] [uniqueidentifier] NOT NULL,
    [VolumeSourceID] [uniqueidentifier] NOT NULL,
    [SolicitationID] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ItemID] [varchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ItemRevision] [varchar](5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [QuoteID] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [SalesOrderID] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [CreationDate] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [AcquisitionDate] [datetime] NULL,
    [LoadedDate] [datetime] NULL,
    [LoadedBy] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ImageCount] [int] NOT NULL CONSTRAINT [DF_Volume_ReadAccessCounter]  DEFAULT (0),
    [LastReadDate] [datetime] NULL,
    [OnlineFlag] [bit] NOT NULL CONSTRAINT [DF_Volume_OnlineFlag]  DEFAULT (1),
    [Path] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [VolumeNote] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ObsoleteCards] [bit] NOT NULL CONSTRAINT [DF_tblVolume_ObsoluteCards]  DEFAULT (0),
    [Blueprint] [bit] NOT NULL CONSTRAINT [DF_tblVolume_Blueprint]  DEFAULT (0),
     CONSTRAINT [PK_Volume] PRIMARY KEY CLUSTERED 
([VolumeID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

CREATE TABLE [dbo].[tblVolumeSource](
    [VolumeSourceID] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_VolumeSource_VolumeSourceID]  DEFAULT (newid()),
    [Mnemonic] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Name] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Location] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [IndexFileName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [VolumeIDFileName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [FormatName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [CD] [bit] NOT NULL CONSTRAINT [DF_tblVolumeSource_CD]  DEFAULT (0),
    [AC] [bit] NOT NULL CONSTRAINT [DF_tblVolumeSource_AC]  DEFAULT (0),
    [Internet] [bit] NOT NULL CONSTRAINT [DF_tblVolumeSource_Internet]  DEFAULT (0),
    [RFQ] [bit] NOT NULL CONSTRAINT [DF_tblVolumeSource_RFQ]  DEFAULT (0),
 CONSTRAINT [PK_VolumeSource] PRIMARY KEY CLUSTERED (
    [VolumeSourceID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]

CREATE TABLE [dbo].[tblVolumeType](
    [VolumeTypeID] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_VolumeType_VolumeTypeID]  DEFAULT (newid()),
    [TypeName] [varchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [TypeDesc] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [CommonSubDirectory] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [CDLoader] [bit] NOT NULL CONSTRAINT [DF_tblVolumeType_CDLoader]  DEFAULT (0),
    [ACLoader] [bit] NOT NULL CONSTRAINT [DF_tblVolumeType_ACLoader]  DEFAULT (0),
 CONSTRAINT [PK_VolumeType] PRIMARY KEY CLUSTERED ( [VolumeTypeID] ASC) WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]

0 个答案:

没有答案