user993460
user993460

Reputation: 821

Rails Model without a table

I want to create a select list for lets say colors, but dont want to create a table for the colors. I have seen it anywhere, but can't find it on google.

My question is: How can I put the colors in a model without a database table?

Or is there a better rails way for doing that?

I have seen someone putting an array or a hash directly in the model, but now I couldn't find it.

Upvotes: 41

Views: 49865

Answers (7)

fguillen
fguillen

Reputation: 38772

I am in Rails 7, but I think this works also in Rails 6:

class MyModel
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :title, :string
  attribute :created_at, :datetime
end

I like this more than the attr_accessor alternative because it allows me to parse attributes to datatypes.

Upvotes: 3

fguillen
fguillen

Reputation: 38772

What worked for me in Rails 6:

class MyClass
  include ActiveModel::Model

  attr_accessor :my_property
end

Upvotes: 6

Vedant Agarwala
Vedant Agarwala

Reputation: 18819

The answers are fine for 2013 but since Rails 4 all the database independent features of ActiveRecord are extracted into ActiveModel. Also, there's an awesome official guide for it.

You can include as many of the modules as you want, or as little.

As an example, you just need to include ActiveModel::Model and you can forgo such an initialize method:

def initialize(attributes = {})
  attributes.each do |name, value|
    send("#{name}=", value)
  end
end

Just use:

attr_accessor :name, :age

Upvotes: 28

Itay Grudev
Itay Grudev

Reputation: 7426

If the reason you need a model without an associated table is to create an abstract class real models inherit from - ActiveRecord supports that:

class ModelBase < ActiveRecord::Base
  self.abstract_class = true
end

Upvotes: 2

Scott S
Scott S

Reputation: 2746

The easiest answer is simply to not subclass from ActiveRecord::Base. Then you can just write your object code.

Upvotes: 11

hkairi
hkairi

Reputation: 385

If you want to have a select list (which does not evolve) you can define a method in your ApplicationHelper that returns a list, for example:

 def my_color_list
   [
     "red",
     "green",
     "blue"
   ]
 end

Upvotes: 1

Louis XIV
Louis XIV

Reputation: 2224

class Model

  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :whatever

  validates :whatever, :presence => true

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end

end

attr_accessor will create your attributes and you will create the object with initialize() and set attributes.

The method persisted will tell there is no link with the database. You can find examples like this one: http://railscasts.com/episodes/219-active-model?language=en&view=asciicast

Which will explain you the logic.

Upvotes: 66

Related Questions