Create a new model;rails, Ruby, Rack without a database table or column

Source: Internet
Author: User

Because in some cases you want to use a nonexistent column, or you want to create a new virtual model

You can create a new tableless.rb under concerns under models, with the following code:

Module tableless
def self.included (Base)
Base.extend (Classmethods)
Base.send (: Include,instancemethods)
End

Module Instancemethods
def save (Validate = True)
Validate? Valid? : True
End
End

Module Classmethods
def column (name, Sql_type = nil, default = nil, NULL = TRUE)
Columns << activerecord::connectionadapters::column.new (name.to_s, default,
sql_type.to_s, NULL)
End

def columns
@columns | | = [];
End
End
End

The use of the model is as follows:

Class Notice < ActiveRecord::Base
Include Tableless

Column:code,: Integer
Column:message,: String

Attr_accessor:code,: Message
End

Create a new model;rails, Ruby, Rack without a database table or column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.