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
7
votes
5 answers
locking rows on rails update to avoid collisions. (Postgres back end)
So I have a method on my model object which creates a unique sequence number when a binary field in the row is updated from null to true. Its implemented like this:
class AnswerHeader < ApplicationRecord
before_save…

GGizmos
- 3,443
- 4
- 27
- 72
7
votes
2 answers
Rails multiple belongs_to for same class
Say I have a model of type A, and a model of type B, which has the fields a_id a2_id. I want to have something like:
class B
belongs_to :a
belongs_to :a (using a2)
end
Does anyone know how I would do this? I am trying to use the class B to link…

joncalhoun
- 1,498
- 1
- 17
- 22
6
votes
3 answers
How to have multiple conditions in a named scope?
I have a User model. I can check whether a User is an admin by doing a_user.try(:admin?).
I'd like to define a named scope that gets all Users updated within the last X minutes who are not admins. So far I have:
scope :recent, lambda { { :conditions…

XåpplI'-I0llwlg'I -
- 21,649
- 28
- 102
- 151
5
votes
1 answer
Undefined method for nil class in Rspec test
So, I have a Game class that can have many Versions, and each Version can have many GameStats. I have my Each Version belongs_to a Game, and each GameStat belongs_to a Version and has_one Game through the Versions. In my tests, when I test for…

Benedict Lee
- 714
- 8
- 21
4
votes
2 answers
How do I Create a Rails Model from a Subset of Table Records
I am trying to create several models that all pull from the same table. How do I limit the table records in each model? And before you tell me to change my data structure, this is a reporting application that is pulling from a preexisting backing…

Bruce P. Henry
- 412
- 1
- 4
- 14
4
votes
1 answer
Rails 3 Nested Model Form
I am having some issues with nested models in a form, using Rails 3.1rc4.
I presently have models that look like this:
class Sale < ActiveRecord::Base
attr_accessible :customer_id, :vehicle_id, :sale_date
belongs_to :customer
…

Harry
- 4,660
- 7
- 37
- 65
4
votes
1 answer
Subclassing activerecord and maintain subclass after db fetch
I have an ActiveRecord model Media which is supposed to be able to store similarly structured information about different types of media (Media::Book, Media::Movie, Media::Music). However each of these subclasses has unique methods.
# TABLE medias
#…

user545139
- 935
- 11
- 27
4
votes
3 answers
nested has_many :through in rails 3
I know Rails doesn't support nested has_many :through relationships, though there's been talk and an open ticket about a patch since as early as Rails 2.
I did come across a plugin that's pretty slick, but the master branches don't work with Rails 3…

Slick23
- 5,827
- 10
- 41
- 72
4
votes
1 answer
Rails 5. Draft version and publish version of model
Which is the best way to have a draft version of a model?
I have a Course model with his description and lesson models.
class Course < ApplicationRecord
has_many :lessons
has_one :description
...
end
class Description < ApplicationRecord
…
4
votes
1 answer
Rails 4 custom generator, revoke/invoke
Given a simple custom generator:
# lib/generators/custom_model/custom_model_generator.rb
class CustomModelGenerator < Rails::Generators::NamedBase
def rails_generate_model
generate 'model', "#{file_name} #{args.join(' ')}"
end
end
Which…

Maitre Renart
- 41
- 3
4
votes
2 answers
Ruby on rails - access model data within another model
I want to access a column from OtherModel.rb to MyModel.rb. Is that possible?
this is what it looks like if the data that I want to access is located within the model it self. and this works just fine
//MyModel.rb
def to_param
self.name
end
but…

TripleAMAC
- 77
- 1
- 1
- 7
4
votes
2 answers
Rails 4 has_many through - Message Sender & Receiver
I'm trying to achieve the following, relationships and object calls
A User can send many messages (user.sent_messages)
A Message can have one Sender (message.sender)
A User can receive many messages (user.received_messages)
A Message can have many…

benlaplanche
- 41
- 4
4
votes
2 answers
Single Table Inheritance with model subdirectories in Rails 3.2.11
I have a model Sport. Using single table inheritance I have 2 other models, Cricket and Football so that
class Cricket < Sport and class Football < Sport
I put these two models in a subfolder inside models directory called sports. I added the type…

user1677428
- 1
- 3
4
votes
2 answers
Ruby on Rails pass id to new create form
Ok, I've searched high and low, read tutorials, watched videos and I am still not getting any where with this. I've read similar questions here, but questions were more complex or lacked answers - so here goes...
I have models Account and Invoice.…

Mark
- 77
- 2
- 7
4
votes
4 answers
Only one instance of a Model in Rails
I'm working on my first Rails app. It needs to store some information about a single advertisement that will appear on every page. The admin just needs to be able to set the URL, Title, and an image. Obviously, I only need one instance of this ad…

Avram
- 103
- 2
- 5