Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby on Your Rails

Ruby on Your Rails

Slides for RubyKaigi 2013 talk "Ruby on Your Rails" http://rubykaigi.org/2013/talk/S84

Akira Matsuda

May 31, 2013
Tweet

More Decks by Akira Matsuda

Other Decks in Programming

Transcript

  1. User.where(name: 'Matz') #=> SELECT * FROM users WHERE name =

    'Matz' User.where('name != ?', 'Matz') #=> SELECT * FROM users WHERE name != 'Matz' SELECT * FROM users WHERE name != 'Matz'
  2. SELECT * FROM users WHERE name != 'Matz' We should

    be able to do this with Hashes We were apparently missing this feature But it wasn't implemented because a good feature needs Proper use case Good API Good implementation
  3. WHERE name != 'Matz' was not in AR3 not because

    it was not possible to implement In fact, it could be done using Arel Objects
  4. WHERE name != 'Matz' was not in AR3 because We

    just couldn't nd a good API Syntax matters (ʮ໊લॏཁʯ)
  5. amatsuda/everywhere A Rails plugin that enables `WHERE + NOT`, `WHERE

    + LIKE` and `WHERE + NOT LIKE` queries Users can switch syntax via con guration
  6. everywhere # :not in Hash value User.where(name: {not: 'Matz'}) #

    :not in Hash key User.where(not: {name: 'Matz'}) # :not + Hash User.where(:not, name: 'foo') # special method User.where_not(name: 'foo')
  7. @tenderlove & @jonleighton I don't like the idea of adding

    "special" hash keys me neither in fact, since I added hstore support, that technique won't work on PG
  8. @bitsweat Perhaps a Model.where with no args should be chainable:

    Model.where.not eld: nil or Model.where.like name: 'John%'
  9. @dhh says Akira Matsuda, where.not is a beaut ul DSL

    solution. Props! Love when a hard problem like that turns out to have a simple DSL solution we just didn't see.
  10. @dhh says imo, I think already like/not_like are going too

    far like/not_like are very rare cases and they just open the door to the monstrocities of gte they're also weird because you need % so it's this bastard version of half sql in the statement, half sql in the parameter Any counter arguments?
  11. Lessons learned Publish your idea as a gem so that

    others can investigate and give a try Jeremy is awesome. He is always right DHH is awesomer (͓͏͞·ʔ) #ବᔬམΫϥϒ AR4 now has where.not feature
  12. More examples class UsersController < ApplicationController def show(id) @user =

    User.find(id) end def create(user) @user = User.new(user) if @user.save ... end end def update(id, user) @user = User.find(id) if @user.update_attributes(user) ... end end end end
  13. @dhh says I was initially excited about it Problems with

    the action_args approach is that it doesn't deal with the preload pattern very gracefully You're passing in a parameter that just says "user", but it's not a user object, like the one assigned to @user, it's a set of parameters for the user. params[:user] is much more clear about what this variable actually contains
  14. I heard that our browsers can also do some form

    validations. Why not on my Rails app?
  15. Good ol' validations class Person # validations start from here

    validates_presence_of :name, :age validates_length_of :name, maximum: 255 validates_numericality_of :age, greater_than: 0 # validations end ... end
  16. validates + block class Person validates do presence_of :name, :age

    length_of :name, maximum: 255 numericality_of :age, greater_than: 0 end ... end
  17. validates + block class Person validates :name do presence length

    maximum: 255 end validates :age do presence numericality greater_than: 0 end ... end
  18. Conclusion Stay on the Edge Be careful and critical Try

    to solve YOUR problems Send your PRs!