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
1
vote
1 answer
Rails - Big vs Distributed Tables
I am relatively new to Rails.
I have a User model through Devise. I am wondering if it is more efficient to have all the additional fields i need for the user, in a separate Profile model.
I am coming across similar situations where I am considering…

alik
- 3,820
- 9
- 41
- 55
1
vote
2 answers
i18n-tasks custom scanner for enums
I would like to create a custom scanner for i18n-tasks that can detect enums declared as hashes in models.
My enum declaration pattern will always be like this:
class Conversation < ActiveRecord::Base
enum status: { active: 0, archived: 1},…

JohnRDOrazio
- 1,358
- 2
- 15
- 28
1
vote
1 answer
Ruby on Rails - concatenate strings on save to database upon user clicking create action
Its late and I probably should sleep on this. Easy one
I have 3 fields in a form which a user fills in. Once they click the create button these records are saved to the database. Simple.
However I want the data from these three fields at the same…

IanN
- 289
- 4
- 13
1
vote
1 answer
Rails 3: update multiple parents when creating a child object on one of the parent's show
I am new to rails and trying to learn now so hopefully someone can help.
I have 3 models for User, Opinion and Vote with one-to-many relationships. Each user has_many :opinions and has_many :votes. Each opinion has_many :votes and belongs_to…

Evgeny
- 13
- 3
1
vote
1 answer
How does rails come up with the ID for a new model/record?
How does activerecord assign an ID to a newly created record? The ID values seem to be all over the place. Sometimes they are sequential, but sometimes they seem to be some kind of a hash.
Is there a way to control the behavior?

EugeneM
- 11
- 1
1
vote
1 answer
Rails 3 Nested Model Form: Can not mass assign protected attribute
I currently have a form set up with nested models - all going according to plan so far. The form allows me to create a sale, and from that I can create a customer and a vehicle (separate models).
The problem comes when I try to create a…

Harry
- 4,660
- 7
- 37
- 65
1
vote
1 answer
cross model scoping - rails 3
I want to create a scope for all Posts without comments... I do not understand how, in the model (by creating a scope), I can check if my Post has any Comments attached to it as only the Comments seem to know what Post they belong to, as opposed to…

fighella
- 580
- 5
- 16
1
vote
4 answers
rails don't find the attribute
I have this model
#post.rb
class Post < ActiveRecord::Base
belongs_to :user
after_initialize :create_token
attr_accessible :token
protected
def create_token
self.token = "#{Digest::MD5.hexdigest("#{self.id}")[0,4]}"
…

JuanPablo
- 23,792
- 39
- 118
- 164
1
vote
2 answers
has_one model association not enforcing 'one' in associated model?
I have the following
class User < ApplicationRecord
has_one :physician
end
and
class Physician < ApplicationRecord
belongs_to :user
end
Since a user can only have one physician, I was surprised that multiple physician records can be…

stevec
- 41,291
- 27
- 223
- 311
1
vote
2 answers
Rails Models relationships
Hello fellow developers!
Recently I've been playing with Rails 3.0 and after quite a bit of research I'm kinda stuck.
I want to know what approach or solution is the best in my case(I couldn't find an answer yet).
So what I'm trying to achieve is…

Alex Bush
- 735
- 1
- 13
- 26
1
vote
2 answers
Joining 2 model's data together and ordering them by common column?
I have 2 models that I am interested in merging and then ordering them by the created_at column. One of the models is photos and the other one is statuses. Currently, I have two tabs, photos and statuses in which I display each of the model data…

Jake
- 13
- 2
1
vote
2 answers
How to add dynamic value for a column in ruby on rails model with Active Record Callbacks
I have a model Document in my rails application. It has columns name, and key.
In my controller for create action, I get document name from the frontend and dynamically set one value for key with securerandom.
The implementation I have for this…

Ashish
- 4,206
- 16
- 45
1
vote
2 answers
Getting part of the raw SQL in Rails API response for not found records
I created a Rails 5 app with config.api_only set to true, and implemented a controller action that deletes a record in a scoped model Message the problem is that when I try to delete a record that doesn't exist, the ActiveRecord::RecordNotFound…

Ivan Garcia
- 154
- 9
1
vote
1 answer
Rails - Problems using Edit
I'm having problems trying to edit a model. I always end up getting as error
undefined method `model_name' for NilClass:Class
I'm using in the view:
<%= form_for(@book) do |f| %>
and on the controller:
def edit
@title = "Edit Book"
end
def…

Zeroz
- 125
- 1
- 8
1
vote
2 answers
Puts statement in function is not executed, function test fails, but receive(:function) spec works
In my model definition, I have
# models/my_model.rb
# == Schema Information
#
# Table name: my_models
#
# id :bigint not null, primary key
# another_model_id :bigint
# field_1 :string
# field_2 …

absolutelydevastated
- 1,657
- 1
- 11
- 28