All Questions
Tagged with model ruby-on-rails
3,428
questions
240
votes
29
answers
134k
views
Is there a way to get a collection of all the Models in your Rails app?
Is there a way that you can get a collection of all of the Models in your Rails app?
Basically, can I do the likes of: -
Models.each do |model|
puts model.class.name
end
213
votes
4
answers
113k
views
Rails :dependent => :destroy VS :dependent => :delete_all
In rails guides it's described like this:
Objects will be in addition destroyed if they’re associated with :dependent => :destroy, and deleted if they’re associated with :dependent => :...
210
votes
5
answers
119k
views
Rails find_or_create_by more than one attribute?
There is a handy dynamic attribute in active-record called find_or_create_by:
Model.find_or_create_by_<attribute>(:<attribute> => "")
But what if I need to find_or_create by more than ...
207
votes
7
answers
158k
views
Determine what attributes were changed in Rails after_save callback?
I'm setting up an after_save callback in my model observer to send a notification only if the model's published attribute was changed from false to true. Since methods such as changed? are only useful ...
129
votes
6
answers
144k
views
Removing a model in rails (reverse of "rails g model Title...")
rails g model Rating user_id:integer message:string value:integer
How can I completely remove this model? Thanks
126
votes
8
answers
117k
views
Using helpers in model: how do I include helper dependencies?
I'm writing a model that handles user input from a text area. Following the advice from http://blog.caboo.se/articles/2008/8/25/sanitize-your-users-html-input, I'm cleaning up the input in the model ...
115
votes
8
answers
93k
views
Rspec doesn't see my model Class. uninitialized constant error
I'm writing tests on Rspec for my models in Ruby on Rails application.
And I receive this error while starting 'rspec spec'
command:
/spec/models/client_spec.rb:4:in `<top (required)>': ...
110
votes
9
answers
111k
views
Rails: Validating min and max length of a string but allowing it to be blank
I have a field that I would like to validate. I want the field to be able to be left blank, but if a user is entering data I want it to be in a certain format. Currently I am using the below ...
106
votes
4
answers
69k
views
Rails "validates_uniqueness_of" Case Sensitivity
Here is the model (I am using SQLLite3):
class School < ActiveRecord::Base
validates_uniqueness_of :name
end
For example, after I add "Yale", I cannot add "Yale" but can add "yale." How can I ...
98
votes
8
answers
73k
views
How to rename rails controller and model in a project
I started a Rails app and everything works fine. But now, I would like to rename a controller and the associated model:
I wanted to change the Corps controller to Stores and the same (without final s) ...
95
votes
10
answers
94k
views
Rails: Update model attribute without invoking callbacks
I have a User model that has a :credits attribute. I want a simple button that will add 5 to the user's credits, through a route called "add" so that /users/3/add would add 5 to the credits of user ...
94
votes
10
answers
68k
views
Using ActiveRecord, is there a way to get the old values of a record during after_update
Setup using a simple example: I've got 1 table (Totals) that holds the sum of the amount column of each record in a second table (Things).
When a thing.amount gets updated, I'd like to simply add the ...
84
votes
5
answers
58k
views
How to access URL helper from rails module
I have a module with a function. It resides in /lib/contact.rb:
module Contact
class << self
def run(current_user)
...
end
end
end
I want to access the URL helpers like '...
80
votes
1
answer
27k
views
Rails 4: organize rails models in sub path without namespacing models?
Would it be possible to have something like this?
app/models/
app/models/users/user.rb
app/models/users/education.rb
The goal is to organize the /app/models folder better, but without having to ...
79
votes
7
answers
32k
views
Using multiple PostgreSQL schemas with Rails models
I have a PostgreSQL database for my Rails application. In the schema named 'public' the main Rails models tables are stored etc. I have created a 'discogs' schema which will have tables with names ...
77
votes
16
answers
59k
views
Rails before_validation strip whitespace best practices
I would like my User model to sanitize some input before before save. For now some simple whitespace stripping will do.
So to avoid people registering with "Harry " and pretend to be "Harry", for ...
68
votes
3
answers
26k
views
How to know whether a model is new or not?
class Post < ActiveRecord::Base
end
post = Post.new
How do I judge whether the 'post' is a new model which is not pulled from the database?
66
votes
8
answers
52k
views
Rails after_initialize only on "new"
I have the following 2 models
class Sport < ActiveRecord::Base
has_many :charts, order: "sortWeight ASC"
has_one :product, :as => :productable
accepts_nested_attributes_for :product, :...
59
votes
6
answers
65k
views
Rails has_many with dynamic conditions
What I want is to create a Model that connects with another using a has_many association in a dynamic way, without the foreign key like this:
has_many :faixas_aliquotas, :class_name => 'Fiscal::...
58
votes
3
answers
78k
views
Ruby on Rails: errors.add_to_base vs. errors.add
I have read that errors.add_to_base should be used for errors associated with the object and not a specific attribute. I am having trouble conceptualizing what this means. Could someone provide an ...
57
votes
6
answers
38k
views
How do you validate uniqueness of a pair of ids in Ruby on Rails?
Suppose the following DB migration in Ruby:
create_table :question_votes do |t|
t.integer :user_id
t.integer :question_id
t.integer :vote
t.timestamps
end
Suppose ...
53
votes
2
answers
10k
views
How to translate model in Ruby class/module namespace?
I have a model Products::Car. How can I translate its attributes?
I have already tried this:
activerecord:
models:
products:
car: "Автомобиль"
attributes:
products:
car:
...
51
votes
8
answers
25k
views
Rails Model has_many with multiple foreign_keys
Relatively new to rails and trying to model a very simple family "tree" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is basically what I want to do, ...
51
votes
3
answers
51k
views
Rails model validation on create and update only
If I want to have validation only on create, then I can do
validates_presence_of :password, :on => :create
But how do I say on create and update? I tried this but it didn't work:
...
50
votes
4
answers
20k
views
How do I prevent deletion of parent if it has child records?
I have looked through the Ruby on Rails guides and I can't seem to figure out how to prevent someone from deleting a Parent record if it has Children. For example. If my database has CUSTOMERS and ...
50
votes
6
answers
28k
views
Create Ruby on Rails views (only) after controllers and models are already created
I've obtained a project that have controllers (minimal code only) and models, but the views are missing. Is there a way to generate the views only using scaffold or another tool?
44
votes
4
answers
59k
views
How does one add an attribute to a model?
In rails I generate a model with two strings and would like to add more. How would I go about doing this?
43
votes
6
answers
8k
views
Ruby on Rails: Is it better to validate in the model or the database?
Is it generally better practice (and why) to validate attributes in the model or in the database definition?
For (a trivial) example:
In the user model:
validates_presence_of :name
versus in the ...
42
votes
3
answers
77k
views
Call a model method in a Controller
I'm have some difficulties here, I am unable to successfully call a method which belongs to a ProjectPage model in the ProjectPage controller.
I have in my ProjectPage controller:
def index
@...
42
votes
5
answers
19k
views
guidelines for where to put classes in Rails apps that don't fit anywhere
I'm wondering if there are any best practices about where to put non-standard Ruby files in Rails apps, those that don't fit in any of the default directories (controllers/models etc.).
I'm talking ...
40
votes
4
answers
30k
views
How to implement Active Record inheritance in Ruby on Rails?
How to implement inheritance with active records?
For example, I want a class Animal, class Dog, and class Cat.
How would the model and the database table mapping be?
40
votes
2
answers
29k
views
Could not find the association problem in Rails
I am fairly new to Ruby on Rails, and I clearly have an active record association problem, but I can't solve it on my own.
Given the three model classes with their associations:
# application_form....
40
votes
6
answers
12k
views
rails model has_many of itself
I have a event model. Events can have parent events, set from a column in the model (parent_event_id). I need to be able to do has_many :event on the model, so I can just do, for example, event....
40
votes
3
answers
25k
views
Rails Internationalization (I18n) in model validations: Possible or not?
I have the following validation in a model:
validates_inclusion_of :whatever, :in => [true, false], :message => I18n.t('please_select_whatever')
It seems that the translation does not work in ...
39
votes
6
answers
9k
views
Rails Models: how would you create a pre-defined set of attributes?
I'm trying to figure out the best way to design a rails model. For purposes of the example, let's say I'm building a database of characters, which may have several different fixed attributes. For ...
36
votes
4
answers
28k
views
Rails: Serializing objects in a database?
I'm looking for some general guidance on serializing objects in a database.
What are serialized objects?
What are some best-practice scenarios for serializing objects in a DB?
What attributes do you ...
35
votes
4
answers
19k
views
Rails: belongs_to vs has_one
A bit of a newbie question on rails associations.
I have a Bug model, and a Status model. Status is basically just a key/value pair table. Out of the choices available, I would say Bug has_one Status ...
34
votes
3
answers
28k
views
rails validate in model that value is inside array
I have a form where i pass a field named :type and i want to check if it's value is inside an array of allowed types so that no one is allowed to post not-allowed types.
the array looks like
@...
34
votes
10
answers
22k
views
How do I validate that two values do not equal each other in a Rails model?
I have a User model, which has an email and a password field. For security, these may not be equal to each other. How can I define this in my model?
33
votes
6
answers
14k
views
Rails: how to require at least one field not to be blank
I know I can require a field by adding validates_presence_of :field to the model. However, how do I require at least one field to be mandatory, while not requiring any particular field?
thanks in ...
31
votes
2
answers
14k
views
Rails put validation in a module mixin?
Some validations are repetitive in my models:
validates :name, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
validates :name_seo, :length => { :minimum => 2 },...
31
votes
4
answers
14k
views
difference between scaffold and model in Rails
What's the difference between generating a scaffold and generating a model in Rails? What are the advantages/disadvantages of doing either?
31
votes
6
answers
21k
views
Rails: change column type, but keep data
I have a model with a column of type integer which I want to convert to type string. Now I'm looking for the best way to change the column type without losing the data. Is there a painless way to ...
30
votes
3
answers
5k
views
problem with passing booleans to update_attributes
I've got the following Model:
class GuestCatering < ActiveRecord::Base
# Validation
validates :name, :presence => true
validates :order_number, :presence => true
validates :...
29
votes
6
answers
10k
views
How to organize Rails models that are too fat?
It is good practice to shift logic from the controller into the model. But in any complex system, this invariably leads to a very large file even if a majority of the methods are one liners as per the ...
28
votes
4
answers
18k
views
Rails table_name_prefix missing
I have got the following dir structure
models/foo/setting.rb
models/foo.rb
foo.rb content
module Foo
def self.table_name_prefix
'foo_'
end
end
and setting.rb content
class Foo::Setting &...
27
votes
5
answers
1k
views
How to model interpretations of rap music
I just started working on a website that will help people understand what rappers are talking about. Users will see the lyrics to a rap song and they'll be able to click certain lyrics to see an ...
25
votes
5
answers
12k
views
validate at least one in has_and_belongs_to_many
I have a model with:
has_and_belongs_to_many :users
How do I validate that the model has at least one user in the model? I tried:
validates_presence_of :users
But that doesn't seem to give me ...
25
votes
2
answers
9k
views
Creating a gem that contains rails models
I've been reading a lot on the topic and nothing seems to quite cover my needs. I'm sorry if I'm repeating or unclear about something I'm both new to ruby and rails and new to Stack Overflow.
I have ...
24
votes
2
answers
11k
views
Is it possible to use rails image_tag from inside a model?
I have a rails 3.x application that has model classes which return content to a view partial to be rendered on a page. The model takes care of what the content is and just returns it to the view which ...