ruby-on-rails-3 interview questions
Top ruby-on-rails-3 frequently asked interview questions
I have the following Rails 3 migration file db\migrate\20100905201547_create_blocks.rb
How can I specifically roll back that migration file?
Source: (StackOverflow)
i.e. delete all the files it created and roll back any changes made? Not necessarily to the db, but more to the config files.
E.g. automatically deleting all the resource mappings for the model/controller deleted in the routes.rb file and everywhere else that changes might have been made?
Thanks.
Source: (StackOverflow)
I added a table that I thought I was going to need, but now no longer plan on using it. How should I remove that table?
I've already ran migrations, so the table is in my database. I figure rails generate migration
should be able to handle this, but I haven't figured out how yet.
I've tried
rails generate migration drop_tablename
, but that just generated an empty migration.
What is the "official" way to drop a table in Rails?
Source: (StackOverflow)
I need to change my column type from date to datetime for an app I am making. I don't care about the data as its still being developed.
How can I do this?
Source: (StackOverflow)
I wrongly named a column hased_password
instead of hashed_password
.
How can update database scheme with using migration to rename this column?
Source: (StackOverflow)
I am using rails 3.1 pre version. I like to use PostgreSQL but the problem is installing the pg gem. It gives me the following error:
demonchand@system-001:~/exercise/personal/pro$ gem install pg
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/home/demonchand/.rvm/rubies/ruby-1.9.2-p0/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/home/demonchand/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
--with-pg
--without-pg
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
Gem files will remain installed in /home/demonchand/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0 for inspection.
Results logged to /home/demonchand/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0/ext/gem_make.out
Can anyone tell me how do I solve this problem?
Source: (StackOverflow)
I recently upgraded to OSX 10.7, at which point my rails installation completely borked when trying to connect to the psql server. When I do it from the command line using
psql -U postgres
it works totally fine, but when I try to run the rails server or console with the same username and password, I get this error
...activerecord-3.0.9/lib/active_record/connection_adapters/postgresql_adapter.rb:950:in `initialize': could not connect to server: Permission denied (PGError)
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
Any ideas what might be going on would be super helpful! Thanks!
Source: (StackOverflow)
I use RVM, the Ruby Version Manager to specify a Ruby version and a set of gems for each of my Rails projects.
I have a .rvmrc
file to automatically select a Ruby version and gemset whenever I cd
into a project directory.
After installing RVM 1.19.0, I get a message
You are using .rvmrc
, it requires trusting, it is slower and it is
not compatible with other ruby managers, you can switch to
.ruby-version
using rvm rvmrc to [.]ruby-version
or ignore this
warnings with rvm rvmrc warning ignore
/Users/userName/code/railsapps/rails-prelaunch-signup/.rvmrc
,
.rvmrc
will continue to be the default project file in RVM 1 and RVM
2, to ignore the warning for all files run rvm rvmrc warning ignore
all.rvmrcs
.
Should I continue using my .rvmrc
file or should I switch to a .ruby-version
file? Which is optimal? What are the ramifications?
Source: (StackOverflow)
I created a new Rails project using rails 3.1.0.rc4
on my local machine but when I try to start the server I get:
Could not find a JavaScript runtime. See here for a list of available runtimes. (ExecJS::RuntimeUnavailable
)
Note: This is not about Heroku.
Source: (StackOverflow)
I'd like Bundler to load a local gem. Is there an option for that? Or do I have to move the gem folder into the .bundle directory?
Source: (StackOverflow)
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 => 'Sierra', :year => "2012", :looks => "Super Sexy, wanna make love to it")
#other processing
@car.save
BTW, I know I can @car.model = 'Sierra'
, but I want to update them all on one line.
Source: (StackOverflow)
I'm new to Rails so my current project is in a weird state.
One of the first things I generated was a "Movie" model. I then started defining it in more detail, added a few methods, etc.
I now realize I should have generated it with rails generate scaffold
to hook up things like the routing, views, controller, etc.
I tried to generate the scaffolding but I got an error saying a migration file with the same name already exists.
What's the best way for me to create scaffolding for my "Movie" now? (using rails 3)
Source: (StackOverflow)
Since the latest Rails 3 release is not auto-loading modules and classes from lib anymore,
what would be the best way to load them?
From github:
A few changes were done in this commit:
Do not autoload code in *lib* for applications (now you need to explicitly
require them). This makes an application behave closer to an engine
(code in lib is still autoloaded for plugins);
Source: (StackOverflow)
To add a new pair to Hash I do:
{:a => 1, :b => 2}.merge!({:c => 3}) # => {:a=>1, :b=>2, :c=>3}
Is there a similar way to delete a key from Hash ?
This works:
{:a => 1, :b => 2}.reject!{ |k| k == :a } # => {:b=>2}
but I would expect to have something like:
{:a => 1, :b => 2}.delete!(:a) # => {:b=>2}
It is important that the returning value will be the remaining hash, so I could do things like:
foo(my_hash.reject!{ |k| k == my_key }
in one line.
Source: (StackOverflow)