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
3
votes
0 answers
Nested Attributes not Working in Rails Form
I have a form that uses two models - badge and badge winners. The user_id and community_id fields are needed for badge_winners which is nested in badges. I was trying to learn from Ryan Bates' Railscast…

yellowreign
- 3,528
- 8
- 43
- 80
3
votes
1 answer
RAILS User hierarchies; classes, associations. Polymorphic?
I'm amazed at how hard it is to still find a definitive answer to this: it seems so common that I must be looking at it all wrong.
We have Users, who's authorization roles run something like
ROLES = %w[admin moderator teacher student…

Ed Jones
- 653
- 1
- 4
- 19
3
votes
2 answers
From Django to Rails, How do you continually work with rails models?
Recently I converted from django development to fulltime rails work, it is a fairly small shop and I'm picking things up from books and on my own as I go.
Last week I was hit with a major blow to my mental model when I learned that rails' models do…

Chris King
- 63
- 7
3
votes
1 answer
Ruby on Rails: How to model a "user favorite model"
I'll use StackOverflow as my example. Let's say I have a Question model. A logged in user can "star" a Question to mark is as one of their favorites. In the database, this sort of thing would probably be stored in a UserQuestions table with a…

Andrew
- 227,796
- 193
- 515
- 708
3
votes
1 answer
Rails 3 model with belongs_to different tables exclusively in each record
I have some models like these:
class Alpha < ActiveRecord::Base
has_many :items
end
class Beta < ActiveRecord::Base
has_many :items
end
class Item < ActiveRecord::Base
belongs_to :alpha
belongs_to :beta
end
But i want Item model in each…

dotoree
- 2,983
- 1
- 24
- 29
3
votes
2 answers
Ruby on rails return a string if a model attribute is nil
I have a user model that has one profile. The profile has attribute such as name, gender, age, dob, designation. No fields in profile are mandatory.
The user is created by an admin from only an email address and a password. All the profile fields…

sonm10
- 53
- 2
- 6
3
votes
2 answers
How to override the default primary key column in ruby on rails 4.0.+?
I have an already existing database schema with tables that have a string column as primary key and also some tables with more than one columns as key. I would like to map this schema in rails, but I dont know how to override the default primary key…

software knife
- 79
- 2
- 8
3
votes
2 answers
Rails: Validation fails for ActiveRecord in setting a random Boolean Attribute
I am trying to save a boolean value in a model by seeding it
20151130014042_create_duty_statuses.rb
class CreateDutyStatuses < ActiveRecord::Migration
def change
create_table :duty_statuses, :id => false do |t|
t.string :id, limit: 36,…

james
- 515
- 4
- 14
3
votes
1 answer
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection
I have the following models:
class Order < ActiveRecord::Base
has_many :order_products, foreign_key: 'order_foreign_id'
has_many :order_variation_values, through: :order_products
accepts_nested_attributes_for :order_variation_values
end
class…

ViT-Vetal-
- 2,431
- 3
- 19
- 35
3
votes
1 answer
Rails 4 - best_in_place gem and many-to-many association
I am struggling to add the ability to update a contact languages using best_in_place (a jQuery inplace-editor gem).
I have a contact object, a language object and a has_and_belongs_to_many :languages relationship between contacts and languages.…

Liyali
- 5,643
- 2
- 26
- 40
3
votes
2 answers
Is it safe to put my model ids into my forms?
I remember seeing somebody say something in some comments somewhere that it isn't very safe to expose the id of a model record to a web page. I tried looking for some ways around it but couldn't find any specific documentation on the likes.
Does…

CoderDake
- 1,497
- 2
- 15
- 30
3
votes
1 answer
Rails Associations Riddle: User, Lecturer, Student and Event
I've googled, read the docs, and watched railscast, but I can't wrap my head around this.
Please help. I'm running the latest stable Rails and Ruby 1.9.3, on Ubuntu 13.04.
I have a User model, and an Event model.
Right now, there is a…

Nadeem J.Q.
- 163
- 1
- 2
- 7
3
votes
0 answers
after_save not called on Heroku production but works locally
I've a model called Rating that reference itself.
class Rating < ActiveRecord::Base
has_many :ratings, :as => :ratable, :dependent => :destroy
after_save :update_total_rating
private
def update_total_rating
return true unless…

Mohammed Khatib
- 31
- 1
3
votes
2 answers
Can't create belongs_to association in controller
I have 2 models Users and Companies. (I'm using Devise for User)
User belongs to Company.
Company has many Users.
My User model includes an client_id column.
At the moment a User signs-up and is directed to the new_company_path where I'd like to…

cupcakekid
- 233
- 3
- 13
3
votes
1 answer
How to validate a Rails model against an enum?
Similarly to how mongoose for nodejs has an enum validator whereby the value of the field must be one of the values in a predefined array, how would a mongoid model in Rails replicate this behavior?
ie.
field :category, type: String --> must be in…

arcyqwerty
- 10,325
- 4
- 47
- 84