Rails, Javascript, Programming, etc
validates_presence_of and accepts_nested_attributes_for
I had to use this. Is accepts_nested_attributes_for
evil? Especially when used with a backbone view?
/blog/index.xml
validates_presence_of and accepts_nested_attributes_for
I had to use this. Is accepts_nested_attributes_for
evil? Especially when used with a backbone view?
Developers coming from Perl or Php often bring conventions from those languages into Ruby. One important thing I like to tell new Ruby developers is that classes in Ruby are cheap so use them liberally. Ruby is an Object Oriented language and objects are the best way to communicate your intent to future developers reading your code. Let’s look at examples of where a class is more appropriate.
First, we have a method returning multiple values for status.
class ExternalOrderService
def handle_order(order_status)
# stuff here
if order_status.include?('Fail on Shipment')
message = 'Order Failed because of Shipment'
success = false
elsif order_status.include?('Order Processed')
message = 'Order Failed because of Shipment'
success = false
else
message = 'Order Failed for unknown reason'
success = false
end
[message, success]
end
end
I see this in the code of others
fields = [ :last_name, :first_name, :order_number, :total, :order_date, :ship_date ]
Please don’t waste :
‘s and commas because as of Ruby 2, you can simply do this:
Ruby has some nice ways to multi line strings. First, this one is the worst. If you are coming from Java or Javascript you might be tempted use it though.
# Do not do it this way
str = 'Lorem ipsum dolor sit amet, duo nusquam minimum id, ius suas elitr ' +
'persius eu. Mel tamquam verterem inciderint in. Solum propriae cum ut.' +
' Cum utinam nonumes nominavi eu, mazim dolor per in.' +
"\n" +
'Debet vivendo pri ei, nec hinc labore in. Duo ad vocibus oporteat ' +
'appellantur. Nibh idque no eos, mel viris partiendo ei, te pro ' +
'discere diceret. Vero aliquid quo an. Porro lobortis convenire vis ' +
'ea, copiosae epicurei percipit nam ut.'
10 Most Common Mistakes that Rails Programmers Make
Can we add using global variable in named scoped in active record?
I recently discovered a cool function in MySQL that is nice for doing group by
queries where you want to return a column with a comma separated list of data to sum up totals. I learn best from examples so let’s look a 3 table example.
Products
CREATE TABLE products (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
)
Inventory Items
CREATE TABLE inventory_items (
id int(11) NOT NULL AUTO_INCREMENT,
product_id int(11) NOT NULL,
sku varchar(255) NOT NULL,
PRIMARY KEY (id)
)
Orders
CREATE TABLE orders (
id SERIAL,
inventory_item_id INTEGER NOT NULL,
amount INTEGER NOT NULL,
PRIMARY KEY (id)
)
The Freelancers’ Show on Learning
A good discussion about self directed learning. I’m definitely more of a “Just in Time” learning unless it’s a topic I’m really interested in.
If you have a large database that you frequently dump and rebuild, you might want to consider using the mysql SOURCE
command. In the past, with smaller databases, I have imported using the following method:
mysql -u root -p monkseal_development < monkseal_development.sql
However, if monkseal_development.sql
is large, there’s a way to speed this up.
mysql --max_allowed_packet=128M -u root -p monkseal_development
....
mysql> SOURCE monkseal_development.sql