Nomethoderror talk

400 views
354 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
400
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Nomethoderror talk

  1. 1. NoMethodErrorMonday, June 10, 13
  2. 2. SUBLIMETEXT MULTIPLECURSORS• CMD+left• Alt+left• selection•Copy/Paste• CMD+d•Ctrl+CMD+gMonday, June 10, 13
  3. 3. GIT REFLOG AKA UNDO ANYTHINGSHIT!Monday, June 10, 13
  4. 4. ‘DEBUGGING’ STUFFWITHOUT DOCSexport BUNDLER_EDITOR=sublbundle open railsuse Sublime Find in filesTudi varianta:git clone ....Gemfile:gem :devise, path:‘/home/user/stuff/devise’Monday, June 10, 13
  5. 5. rake db:create db:migrate db:seedor use Zeus and don’t careMonday, June 10, 13
  6. 6. MEMOIZATIONTRICKSdef data@data ||= begindata = fetch_data("http://www.com")JSON.load(data)rescue Errno::ECONNREFUSED{ error: Connection refused. }endend###def data@data = get_data unless instance_variable_defined?(:@data)enddef get_data# ...rescue# ...endMonday, June 10, 13
  7. 7. rails g model Fart ass:referencesrails g model Fart ass:belongs_toautomatically adds indexautomatically puts belongs_to into modelclass CreateFarts < ActiveRecord::Migrationdef changecreate_table :farts do |t|t.references :ass, index: truet.belongs_to :ass, index: truet.timestampsendendendclass Fart < ActiveRecord::Basebelongs_to :assendMonday, June 10, 13
  8. 8. RUBY INSTRUCTIONSEQUENCEMonday, June 10, 13
  9. 9. PRESENT?def show@user = User.find(params[:id])if @user.present?# ...endenddef show@user = User.find(params[:id])if @user# ...endendSTOP ABUSING!Monday, June 10, 13
  10. 10. PRESENCE"".presence # => nil"X".presence # => "X"[].presence # => nil[1].presence # => [1]Monday, June 10, 13
  11. 11. ACCEPTS_NESTED_ATTRIBUTES_FORbundle open railsimplement custom version in our appMonday, June 10, 13
  12. 12. ADD METHODS WITH SCOPEclass Fart < ActiveRecord::Basescope :page, ->(page = 1) { where(page: page) } dodef per(per = 10)limit(per)endendendFart.per(20)# NoMethodError: undefined method `perFart.page(2).per(20).to_sql# => "SELECT "farts".* FROM "farts" WHERE "farts"."page" = 2LIMIT 20"Kaminari does this: .page(...) adds .per(...)Monday, June 10, 13
  13. 13. GROUP COUNT# Useless:Fart.group(:ass_id).count # => { 1 => 1, 2 => 1 }# Slow:Fart.group(:ass_id).count.keys.count # => 2# Fast + should work for any query:scope = Fart.group(:ass_id).select("1")query = "SELECT count(*) AS count_all FROM (#{scope.to_sql}) x"ActiveRecord::Base.connection.execute(query)["count_all"].to_i # => 2# Gem: mrbrdo/active_record_group_countFart.group(:ass_id).returns_count_sum.count # => 2# Works with KaminariFart.group(:ass_id).returns_count_sum.page(1).per(10)Monday, June 10, 13
  14. 14. RAILS 3 ASSIGN_ATTRIBUTESWITHOUT PROTECTIONPS: use strong_parameters (even in Rails 3), attr_accessible sucksFart.new(name: lol) # ActiveModel::MassAssignmentSecurity::Errorfart = Fart.new({ name: lol }, without_protection: true)fart.update_attributes({ name: lol }, without_protection: true)fart.assign_attributes({ name: lol }, without_protection: true)be carefulMonday, June 10, 13
  15. 15. RAILS CONSOLE SANDBOXMonday, June 10, 13
  16. 16. Monday, June 10, 13

×