a component of the Rails framework that holds the state of an object and is responsible for enforcing business rules and persisting the object.
Questions tagged [rails-models]
367 questions
74
votes
3 answers
Rails has_one :through association
Rails has a has_one :through association that helps set up a one-to-one association with a third model by going through a second model. What is the real use of that besides making a shortcut association, that would otherwise be an extra step…

Anurag
- 140,337
- 36
- 221
- 257
64
votes
4 answers
LoadError Unable to autoload constant Message
In my app; when I submit form, I get this error:
LoadError at /questions
Unable to autoload constant Message, expected /app/models/message.rb to define it
It points to the create action in the Questions controller:
@message =…

pwz2000
- 1,385
- 2
- 16
- 50
55
votes
2 answers
What is the best way to drop a table & remove a model in Rails 3?
I have a model & a table which I no longer need in my App, I could leave them there but I would like to remove them to keep things tidy.
I'm trying to figure out the best way to remove them with out messing around with my migrations & db/schema.rb…

Holly
- 7,462
- 23
- 86
- 140
53
votes
6 answers
Shared models between two Rails apps - what is the ideal solution for Workflow?
I am currently working on a Rails 3 project that is divided up into four parts:
The public facing website
The administration website/backend
The models
The API for third party data access
As the models are shared between the three key components I…

Matthew Savage
- 3,794
- 10
- 43
- 53
32
votes
6 answers
In Rails, how should I implement a Status field for a Tasks app - integer or enum?
For a Rails 3.0 Todo app, I have a Tasks model with a Status field. What's the best way to store the Status field data (field type) and still display a human-readable version in a view (HTML table)? Status can be:
0 = Normal
1 = Active
2 =…

Doug
- 3,119
- 3
- 27
- 30
20
votes
3 answers
Specify an optional reference in your Rails model
I have a Sponsors model and a Promo Codes model.
A sponsor can have zero or more promo codes
A promo code can have zero or one sponsors
Thus a promo code should have an optional reference to a sponsor, that is, a sponsor_id that may or may not…

Dennis
- 56,821
- 26
- 143
- 139
19
votes
4 answers
How to add sequences to a migration and use them in a model?
I want to have a "Customer" Model with a normal primary key and another column to store a custom "Customer Number". In addition, I want the db to handle default Customer Numbers. I think, defining a sequence is the best way to do that. I use…

Hiasinho
- 656
- 6
- 13
16
votes
2 answers
Rails: Invalid single-table inheritance type error
So, I am working on migrating this php site with an existing database which I cannot change over to Rails. There is a table: Quotes with a column named type. Whenever I try and create a model of this and set the type, it tells me the following…

Blaine Kasten
- 1,633
- 1
- 15
- 27
14
votes
1 answer
Nested attributes for belongs_to association rails
I have two models, Complaint and Company. Complaint belongs_to and accepts_nested_attributes for Company, and Company has_many Complaints.
# Models
class Complaint < ActiveRecord::Base
attr_accessible :complaint, :date, :resolved
belongs_to…

pjmil
- 2,087
- 8
- 25
- 41
13
votes
3 answers
Ruby on Rails: how to migrate changes made on models?
In a Rails application, how can I migrate the changes I make in models?
For instance, I know that if I create a model with command rails g model Person name:string, a migration will be created as well. However, if after this step I go to the created…

Rui
- 5,900
- 10
- 38
- 56
9
votes
3 answers
has_one and has_many in same model. How does rails track them?
I a little confused about how this work even if it works properly. I have a model that has two association to the same other model.
Company has an owner and company has many employees of the class users.
here is my company model:
class Company <…

nkassis
- 5,285
- 2
- 21
- 22
8
votes
3 answers
Test Migrations are not running with my Rails Engine, even though `maintain_test_schema!` is specified
I'm developing a Rails Engine (Plugin). So far I have
Set up RSpec as a framework
Added a migration to create a model/table called MyJob
Added some basic model tests under spec/models/my_job_spec.rb
The rails template automatically creates a…

user2490003
- 10,706
- 17
- 79
- 155
8
votes
2 answers
In Ruby on Rails, if we generated a model "Animal", and now want to have "Dog", how should we do it?
say, if we generated a model
rails generate model animal name:string birthday:date
and now we want to create other model to inherit from it (such as Dog and Cat), should we use rails generate model again or just add the files ourselves? How do we…

nonopolarity
- 146,324
- 131
- 460
- 740
7
votes
1 answer
Namespacing models in a Rails application
I had a discussion recently with a friend of mine who is also a RoR developer. We argued about how Rails models should be managed. Personally I like to leave in the default namespace only the root models (e.g. User, Article, Bill etc.), and the…

FreeCandies
- 1,088
- 10
- 21
7
votes
2 answers
Rails belongs_to not validating id when optional
I'm running Rails 5.1.4, and I have a model that looks like this:
class Quota < ActiveRecord::Base
belongs_to :domain, optional: true
belongs_to :project, optional: true
end
A quota should belong to a domain OR project, but not both (hence…

jsmartt
- 1,404
- 1
- 15
- 22