Posts tagged ruby on rails
How to get and set model data from inside a Rails model
How to get the my_var model variable:
puts self.varor
puts varHow to set the my_var model variable:
self.var = "new value"Note that when setting, you must prefix with self., unlike when getting. Also note that @var will not work for setting or getting model data, ever.
It's okay to make non-RESTful controllers & routes in Rails
As part of my ongoing professional education, I’ve decided to read through several Ruby on Rails projects on Github. It has been a great experience, and I am learning a lot. It’s insightful to see where I’m better than other software architects, and where I have room to grow – there are some very nuanced lessons to learn, and some very simple ones. This is one of the very simple ones.
:dependent => :destroy vs. :dependent => :delete_all in Rails models
- If you use
:dependent => :destroyin your Rails model, each model to be destroyed will be instantiated and thedestroy()method will be called upon it. This will result in the normal object destruction lifecycle, include any callbacks. Destroying children and grandchildren can be handled gracefully. It is a safe, normal way to destroy objects. If you’re not sure whether to use:dependent => :destroyor:dependent => :delete_all, use:dependent => destroy. - If you use
:dependent => :delete_allin your Rails model, no objects will be loaded into memory and nodestroy()methods will be called. Instead, a simple SQL query along the lines ofDELETE * FROM dependent_model where depending_model_id = Nwill be run. There is no concern paid to destroying children and grandchildren; they will be orphaned in the database. If constructing a dependend model object and invokingdestroy()is slow, or you simply wish to bypass the normal object destruction lifecycle, this is your best choice.
Don't use t.integer for referencing related tables in Rails migrations
Instead, use:
create_table :address do |t|
t.references :person
endRails' link_to always gives me a headache
After 9 years of writing Ruby on Rails code, link_to() never ceases to trip me up. Does it trip you up, too? Here are some quick notes:
- The first argument is what you want inside the
<a>tag. For examplelink_to("today's news")produces<a>today's news</a> - The second argument is the
hrefattribute value. For example,link_to("today's news", "www.news.com")produces<a href="www.news.com">today's news</a> - The third argument is a hash that contains everything else – most importantly, your
<a>tag attributes. For example,link_to("today's news", "www.news.com", {id: "newsbox", class: "infos"})produces<a id="newsbox" class="infos" href="www.news.com">today's news</a>. The third argument can contain many other options, of which I have never been able to find a definitive list.
View all valid URL routes in a Rails app
rake routesWhat is the ./bin directory in a Ruby on Rails project?
Introduced in Rails 4, the ./bin directory contains your app’s “binstubs.” Binstubs are wrappers around gem executables, like rails or bundle, which ensures a gem executable is run inside the correct environment for your Rails app.
The Ruby on Rails folder structure
When a new Ruby on Rails project is created with rails new, many files and folders are created inside your project folder. Here’s a quick reference of what those are.
How to list all rake tasks
rake -T -AThis command will print out all available rake tasks. You can use it with Rails’ Rakefile, or with a custom Rakefile. N.B.: not all tasks have descriptions, and that is okay.
How to open the database's CLI in Ruby on Rails
rails dbconsoleShould I use MySQL or PostgreSQL with Ruby on Rails?
PostgreSQL.
(Updated for 2015.)
Using attr_accessor in Rails model classes
- Don’t confuse
attr_accessorandattr_accessible– they have very similar names attr_accessoris a pure Ruby function to create reader and writer methods for an instance variableattr_accessibleis a pre-Rails 4 function to list the model attributes (e.g., database fields) that can be set via mass assignment, providing access control
You can use attr_accessor on a Rails model to create non-persisted instance variables. For example:
class Stock
attr_accessor :current_price
endThis allows you to get the @current_price of a stock – something that would make no sense to persist.
How to ensure multiple ActiveRecord calls happen sequentially
ActiveRecord::Base.transaction do
savings.withdrawal(100)
checking.deposit(100)
endIf any exceptions are raised, the entire operation is rolled back on the database layer and nothing will be committed to the database.
Should I use Turbolinks in Rails?
No.
(Don’t believe me? Google it.)