Questions tagged [ruby-on-rails-4]
For issues specific to version 4 of Rails. If your question applies to Ruby on Rails in general, use the tag [ruby-on-rails].
36,784
questions
834
votes
18
answers
575k
views
How to get a random number in Ruby
How do I generate a random number between 0 and n?
646
votes
6
answers
248k
views
How to use concerns in Rails 4
The default Rails 4 project generator now creates the directory "concerns" under controllers and models. I have found some explanations about how to use routing concerns, but nothing about controllers ...
440
votes
5
answers
348k
views
Rails 4: List of available datatypes
Where can I find a list of data types that can be used in Ruby on Rails 4?
Such as
text
string
integer
float
date
I keep learning about new ones and I'd love to have a list I could easily ...
431
votes
20
answers
179k
views
Rails 4: how to use $(document).ready() with turbo-links
I ran into an issue in my Rails 4 app while trying to organize JS files "the rails way". They were previously scattered across different views. I organized them into separate files and compile them ...
424
votes
4
answers
177k
views
Rails update_attributes without save?
Is there an alternative to update_attributes that does not save the record?
So I could do something like:
@car = Car.new(:make => 'GMC')
#other processing
@car.update_attributes(:model => '...
390
votes
5
answers
64k
views
Rails I18n validation deprecation warning
I just updated to rails 4.0.2 and I'm getting this warning:
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you ...
370
votes
8
answers
406k
views
Add a reference column migration in Rails 4
A user has many uploads. I want to add a column to the uploads table that references the user. What should the migration look like?
Here is what I have. I'm not sure if I should use (1) :user_id, :...
360
votes
6
answers
178k
views
Rails 4: before_filter vs. before_action
In rails >4.0.0 generators creates CRUD operations with before_action not before_filter. It seems to do the same thing. So what's the difference between these two?
265
votes
6
answers
152k
views
How is attr_accessible used in Rails 4?
attr_accessible seems to no longer work within my model.
What is the way to allow mass assignment in Rails 4?
232
votes
4
answers
130k
views
Auto-loading lib files in Rails 4
I use the following line in an initializer to autoload code in my /lib directory during development:
config/initializers/custom.rb:
RELOAD_LIBS = Dir[Rails.root + 'lib/**/*.rb'] if Rails.env....
230
votes
5
answers
67k
views
Paperclip::Errors::MissingRequiredValidatorError with Rails 4
I'm getting this error when I try to upload using paperclip with my rails blogging app.
Not sure what it is referring to when it says "MissingRequiredValidatorError"
I thought that by updating ...
215
votes
17
answers
173k
views
Rails: How to reference images in CSS within Rails 4
There's a strange issue with Rails 4 on Heroku. When images are compiled they have hashes added to them, yet the reference to those files from within CSS don't have the proper name adjusted. Here's ...
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 ...
205
votes
3
answers
109k
views
Rails: validate uniqueness of two columns (together)
I have a Release model with medium and country columns (among others). There should not be releases that share identical medium/country combinations.
How would I write this as a rails validation?
200
votes
13
answers
131k
views
Rails 4 Authenticity Token
I was working on a new Rails 4 app (on Ruby 2.0.0-p0) when I ran into some authenticity token problems.
While writing a controller that responds to json (using the respond_to class method), I got to ...
199
votes
3
answers
145k
views
What is `params.require(:person).permit(:name, :age)` doing in Rails 4?
All the examples of strong parameters in Rails 4 docs use
params.require(:person).permit(:name, :age)
Could someone please deconstruct and explain what is occurring with require and permit here?
191
votes
15
answers
193k
views
How to solve error "Missing `secret_key_base` for 'production' environment" (Rails 4.1)
I created a Rails application, using Rails 4.1, from scratch and I am facing a strange problem that I am not able to solve.
Every time I try to deploy my application on Heroku I get an error 500:
...
161
votes
6
answers
77k
views
A copy of xxx has been removed from the module tree but is still active
I'm pretty sure the error has nothing to do with the actual content of the TenantIdLoader module. Instead, it has something to do with ActiveSupport Dependencies.
I can't seem to get past this error. ...
159
votes
12
answers
38k
views
Rails Observer Alternatives for 4.0
With Observers officially removed from Rails 4.0, I'm curious what other developers are using in their place. (Other than using the extracted gem.) While Observers were certainly abused and could ...
153
votes
10
answers
73k
views
Rails 4 - Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded
In my gemfile I have:
gem 'mysql2'
My database.yml is as follows:
default: &default
adapter: mysql2
database: <%= ENV['db_name'] %>
username: <%= ENV['db_user'] %>
password: ...
151
votes
18
answers
292k
views
cannot load such file -- bundler/setup (LoadError)
I'm setting Rails 4 application with Ruby 2.0, but I'm getting "Web application could not be started" and get this trace:
cannot load such file -- bundler/setup (LoadError)
/usr/local/lib/ruby/...
148
votes
6
answers
104k
views
scopes with lambda and arguments in Rails 4 style?
I'm wondering how the following is done in Rails 4 or if I just use the Rails 3 approach for using a lambda that can pass an argument the same way with 4 as I do with 3.
I'm pretty new to Rails 3 ...
147
votes
29
answers
414k
views
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
Before anything, please note that I have found several similar questions on Stack Overflow and articles all over the web, but none of those helped me fix my issue:
PG Error could not connect to ...
130
votes
6
answers
87k
views
How get integer value from a enum in Rails?
I have a enum in my Model that corresponds to column in the database.
The enum looks like:
enum sale_info: { plan_1: 1, plan_2: 2, plan_3: 3, plan_4: 4, plan_5: 5 }
How can I get the integer ...
128
votes
6
answers
160k
views
Nested attributes unpermitted parameters
I have a Bill object, which has many Due objects. The Due object also belongs to a Person. I want a form that can create the Bill and its children Dues all in one page. I am trying to create a form ...
125
votes
18
answers
122k
views
Rails 4: assets not loading in production
I'm trying to put my app into production and image and css asset paths aren't working.
Here's what I'm currently doing:
Image assets live in /app/assets/images/image.jpg
Stylesheets live in /app/...
121
votes
9
answers
65k
views
Saving enum from select in Rails 4.1
I am using the enums in Rails 4.1 to keep track of colors of wine.
Wine.rb
class Wine < ActiveRecord::Base
enum color: [:red, :white, :sparkling]
end
In my view, I generate a select so the ...
121
votes
6
answers
416k
views
fe_sendauth: no password supplied
database.yml:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: postgresql
encoding: utf8
database: ...
120
votes
3
answers
78k
views
There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError)
Why am i getting this error?
shibly@mybox:~/blog$ rails server
/home/shibly/.gem/ruby/2.2.4/gems/bundler-1.11.2/lib/bundler/runtime.rb:80:in `rescue in block (2 levels) in require': There was an ...
117
votes
8
answers
54k
views
Disable ActiveRecord for Rails 4
I want to disable ActiveRecord in Rails 4. I did the following in config/application.rb
require File.expand_path('../boot', __FILE__)
# require 'rails/all' -- commented
require "action_controller/...
112
votes
5
answers
50k
views
Deprecated warning for Rails 4 has_many with order
class RelatedList < ActiveRecord::Base
extend Enumerize
enumerize :list_type, in: %w(groups projects)
belongs_to :content
has_many :contents, :order => :position
end
I have this ...
109
votes
5
answers
92k
views
Safe ActiveRecord like query
I'm trying to write LIKE query.
I read that pure string quires aren't safe, however I couldn't find any documentation that explain how to write safe LIKE Hash Query.
Is it possible? Should I ...
108
votes
20
answers
46k
views
Error when trying to install app with mysql2 gem
Im trying to install an open source rails 3.2.21 application that uses the mysql2 gem, but when i try and run the bundle commant I get the following error:
Fetching: mysql2-0.3.18.gem (100%)
Building ...
103
votes
7
answers
155k
views
Rails 4 image-path, image-url and asset-url no longer work in SCSS files
Are we supposed to use something else aside from image-url and others in Rails 4? They return different values that don't seem to make sense. If I have logo.png in /app/assets/images/logo.png and I do ...
100
votes
3
answers
24k
views
Why did Rails4 drop support for "assets" group in the Gemfile
In Rails 3, gems used exclusively to generate assets in the asset pipeline were properly placed in the assets group of the Gemfile:
...
# Gems used only for assets and not required
# in production ...
97
votes
2
answers
23k
views
Deprecation warning when using has_many :through :uniq in Rails 4
Rails 4 has introduced a deprecation warning when using :uniq => true with has_many :through. For example:
has_many :donors, :through => :donations, :uniq => true
Yields the following warning:...
97
votes
3
answers
24k
views
how to know what is NOT thread-safe in ruby?
starting from Rails 4, everything would have to run in threaded environment by default. What this means is all of the code we write AND ALL the gems we use are required to be threadsafe
so, I have ...
95
votes
8
answers
63k
views
Rails association with multiple foreign keys
I want to be able to use two columns on one table to define a relationship. So using a task app as an example.
Attempt 1:
class User < ActiveRecord::Base
has_many :tasks
end
class Task < ...
94
votes
4
answers
48k
views
How to override X-Frame-Options for a controller or action in Rails 4
Rails 4 appears to set a default value of SAMEORIGIN for the X-Frame-Options HTTP response header. This is great for security, but it does not allow for parts of your app to be available in an iframe ...
93
votes
18
answers
45k
views
Rails: How to use i18n with Rails 4 enums
Rails 4 Active Record Enums are great, but what is the right pattern for translating with i18n?
91
votes
13
answers
128k
views
LEFT OUTER JOIN in Rails 4
I have 3 models:
class Student < ActiveRecord::Base
has_many :student_enrollments, dependent: :destroy
has_many :courses, through: :student_enrollments
end
class Course < ActiveRecord::...
89
votes
17
answers
89k
views
An error occurred while installing pg (0.17.1), and Bundler cannot continue
I just installed Rails 4.0.2 and when creating a new app, in the bundle stage I get:
Installing pg (0.17.1)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/...
86
votes
5
answers
80k
views
Generate migration - create join table
I have looked through many SO and google posts for generating migration of join table for has many and belongs to many association and nothing work.
All of the solutions are generating a empty ...
86
votes
2
answers
58k
views
In Rails, what's the difference between find_each and where?
In Rails, both find_each and where are used for retrieving data from Database supported by ActiveRecord.
You can pass your query condition to where, like:
c = Category.where(:name => 'Ruby', :...
84
votes
6
answers
76k
views
Rails 4 multiple image or file upload using carrierwave
How can I upload multiple images from a file selection window using Rails 4 and CarrierWave? I have a post_controller and post_attachments model. How can I do this?
Can someone provide an example? Is ...
82
votes
5
answers
45k
views
What is the difference between Workers and Threads in Puma
What is the difference between a puma worker and a puma thread in context of a heroku dyno?
What I know (please correct me if I am wrong):
Thin is not concurrent, so a web process can only do one ...
81
votes
5
answers
44k
views
Active admin install with Rails 4
I got this error when installing active admin on Rails 4
Bundler could not find compatible versions for gem "actionpack":
In Gemfile:
meta_search (>= 1.1.0.pre) ruby depends on
actionpack (~&...
80
votes
7
answers
28k
views
'rails generate' commands hang when trying to create a model
I'm new to rails and decided this morning to dump my whole database design/model and start over. And being a noob, I'm sure did it incorrectly.
I removed all the files in db/migrate/ and dropped the ...
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
2
answers
77k
views
What is the use of secret_key_base in rails 4
I am new to Rails 4, and do not understand the use of secret_key_base under config/secrets.yml in Rails 4. Can you please explain this concept?
Also, when I am working in the production environment, ...