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.var

or

puts var

How 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.

more...

:dependent => :destroy vs. :dependent => :delete_all in Rails models

  • If you use :dependent => :destroy in your Rails model, each model to be destroyed will be instantiated and the destroy() 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 => :destroy or :dependent => :delete_all, use :dependent => destroy.
  • If you use :dependent => :delete_all in your Rails model, no objects will be loaded into memory and no destroy() methods will be called. Instead, a simple SQL query along the lines of DELETE * FROM dependent_model where depending_model_id = N will 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 invoking destroy() is slow, or you simply wish to bypass the normal object destruction lifecycle, this is your best choice.

Rails' 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:

  1. The first argument is what you want inside the <a> tag. For example link_to("today's news") produces <a>today's news</a>
  2. The second argument is the href attribute value. For example, link_to("today's news", "www.news.com") produces <a href="www.news.com">today's news</a>
  3. 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.

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.

more...

How to list all rake tasks

rake -T -A

This 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.

Using attr_accessor in Rails model classes

  • Don’t confuse attr_accessor and attr_accessible – they have very similar names
  • attr_accessor is a pure Ruby function to create reader and writer methods for an instance variable
  • attr_accessible is 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
end

This allows you to get the @current_price of a stock – something that would make no sense to persist.