Add to Watchlist

Rails 5 Features You Haven't Heard About


Citation of segment
Embed Code
Purchasing a DVD Cite video

Automated Media Analysis

Recognized Entities
Speech transcript
thank you for all and that you would do that and the full because what's going I think we know and it started here this is the features in rails slide you haven't heard about my alternate title was shown dramatically reads that the change log but I don't have a great dramatic voice so instead of solution undramatically reads the changelog no actually that the main focus of this talk is less about the features themselves and more about the stories that went implementing them this is that we focus on the smaller quality of changes those who saw things I get excited about every every time the rails updates yeah when they have they get started if I go to pressing buttons on the keyboard of the a way that the right it's either with every version of rails it is the smaller quality-of-life of features the things that are going to fundamentally change how build applications but the things are gonna make my life to slow the easier smaller tools the on new protections performance improvements a lot of them have very interesting stories that went into why they were added or in some cases where they were added so that organism my name John Griffin I work full time on open source and the committer on Ruby on Rails match up by sponsors we do open-source full-time so thank you shop by and as I was saying every feature has a story of a story about I what went and planting it why was added and case of things like relation or why they weren't added for as long as they as they work under the 1st feature that like talk about is 1 of spoken a lot about the past and it's the type actually API and this one's actually kind of interesting to put thing about rails 5 because most of the work that went into implementing this feature was actually done in rules for to I mean real sporty we we revamped the entirety of how we can take origin Active Record in preparation for the public API and the guys 90 % implemented in 4 and 4 . 2 with a handful edge cases of and I have talked at length about this in the past but I don't ever told the story of why this feature is added and there are a lot of things that that led up to it but it really there was 1 particular project that stood out as as the thing that really convinced me that this was a particle-mediated I was something was working on was still a and the project has the requirement that all data needed to be encrypted at rest if our our database credentials will be so if you actually had access to the database is still needed to be unable to to read anything so we were using a gentle called adder attracted to start and this is what I use mentioned looks like you call an accepted and you give that name of an entity and it will assume that the database column is called that we name underscore encrypted it defines the reader and a writer for the unencrypted form and performs encryption in Ruby of the problem there is that it doesn't work with methods like where or find by now there was a feature of the time this since then removed and this is example of the code that doesn't work on the there is an escape hatch they used to give you that since been removed in that the dynamically defined finder methods of such as this this form of were defined but this is how we tend to write of rails applications anymore and haven't since Rails to 3 so using this gem would mean that our entire application would have to change to know that this field happened be encrypted in the database but now we ended up using gems like ransacked for complex search and so we ended up having to that should take iden and that fixed even fixing what this specific project needed because we need to be able do other things like less than war like which doesn't work terribly well if it's if something is encrypted and so he ended up writing this massive hack using the PG crypto extension and we would walk the entire Errol AST looks for any binary nodes where the left side was an error let's review and I replace it with an interest in a different in it and it was some of those that reduce code that ever written in and around all this time this guy I gave a talk at real something half a so there's a picture that needs a at early Miller's giving a talk about some of the from pure parts of the Active Record internals i and 1 of things that he was mentioning was that it would be really nice if 1 Active Record defined the excessive methods to match a database schema if it was just calling some public API for you automatically the and it agreed to update its own a lot like what I've been looking to do that for quite some time and I really agreed with his sentence on how to work under the hood and the so then we fast-forward 8 months and I went and got this we got into rails to start atoms feature and vastly underestimated the amount of work that is going to be doing on it but we had a working implementation of the API pretty quickly but the thing that bothered me about it was at the time typecasting rules 4 . 1 typecasting happens by grabbing the column objects associated with that field I phoned typecast method and types in for 1 basically just represented as a symbol when reloading from the database we look at the it's struck the sequel string that represents the type in the database and we create a simplified type from that which is represented as a symbol it would have a case statement on the symbol and then i it would choose what behavior have based on that and and so the initial implantation got rid of the
case statement replace it with us some objects that we could inject but ultimately the way this worked was typecasting selected columns in 4 2 and 1 earned in early implications of it and we're when you call the entries API at that point we would actually creating new column objects and 6 then the columns hash and would base and would basically lie and is especially bothered me if it was not being used for something that wasn't backed by a database field because there's not even a column involves there are and so there's a lot of completed concerns about what a column versus and actually was and so getting the initially the cat is actually pretty simple were fast at least then there's another 8 months of work to get an implementation I was really happy with and it required rewriting just about every part of active record with the exception of of association code was touched by this change in some way but now now we we have finally gotten to a point where I it's it's a it's a very clean and simple implantation and this is what the the define a of it just looks like you call the class matter actually if you are given the name of the of the actually and you give the type of objects and I like to give examples were passing explicit type object especially when constructor arguments but this was 1 thing that I really want to go for a with a with what I wanted you can refer to as my overly engineered extremely complicated replace the predecessor half but I want to work with objects here because the semantics of how you do things like dependency injection are very clear when you just passing an object instead of having a magic DSL the symbols and registries and this is a of universal API so you cannot what 1 the things I try to do when when restructuring the code was we still needed to get type information from a column at some point we still have to know when you look for model go ask that they go ask the database schema and then call the a for you automatically but we still at some point we need to get the take information from the column which is kind of sort of help from 4 . 1 and so I really want to make sure that the code was engineered in such a way that other people getting into the codebase wouldn't mistakenly I I use the column object in that way that and that there was only 1 way to get type information and that's why when the big things 1 big benefits of athletes HI over justifying reader and writer is that it works with methods like where if you have a price column in representing as a money object you'll probably want to be able to do where price for 1 and then passes in many objects this is what the the schema inference look looks like it's exactly as we're ascribing it but we just looked through all or columns and we just call publicly categories are defined actually gives us also public API it's a little different actually in that it is its strict res attribute is lazy because it waits until columns of it onto we've done this code so they can override what the database said if you ask if you assistive anyway so that the reason the took so long was because I didn't wanna ship it when I was unhappy with the implantation in particular the fact that we were modifying the column objects was technically visible through public API and I don't want me to that but now we're in real fine and others will finally be of features and available but it was what got me into open source and why I decided that we needed another full-time rails so it's got a special place in any way i talked a lot about this in the past and all want spend too much time on its let's move on to the single most frequently requested feature since Rails 3 I've been seems proposed so many times and it was it was just I'm sorry it took so long to it to get but I'm really excited about active support left and few finally finally we provide you a way to add value to the left side of the string and we feel that this feature is so important that we will actually be shipping this as a separate GM independently of rails it and we're really hoping and everybody in the in the review ecosystem depend on the screen in future sorry I don't know that uses a nova this probably actually is the most requested feature on active regulation or finally of 6 years after relations introduced you can add and or expression your where clause that if few right and so the response like that this feature that begs the question what took so long why did we just do this in the 1st place and there's a number of reasons but the biggest 1 is that the API is actually much less obvious imminent but 1 thing that that people often forget when they're doing with open source projects is that it's extremely difficult to change anything I to remove EPI African introduced the we can just do something that's good enough for that gives the escape hatch right now almost were really confident that that is not something that will get in the way data so we really the cop and that was the right solution this is the membership it takes a relation and it it's method on relation takes another relation as the argument and i and so this is sort of the intended use case if we have 2 names scopes once called recent and 1 is called hand we want anything as recent work and be on the front page of a blog of the neutrino scope and you do recent work in and this is uh sort of the the key to why we why we did this specific design is that we wanted to allow you to read username scopes if something appears as half of a network is likely to be used as an independent independently of the or and so
we really want to optimize for named scopes or more importantly we want optimize the composition and abstraction we want to give you a tool that lets you write code that was easy to reuse easy to change and names so howling Casserly queries and there a bunch of other proposals this again this is probably the most common 1 that we saw where it was just warranted passionate they behaves exactly where others couple big problems with this so the 1st was that were doesn't imply where at all I in deciding the only place in support can appear for can appear inside of there having class on since relation it's sort of exists in the realm of set theory it would also be entirely reasonable to think that war meant Union query and so some people that the the next most of those all up solve that problem by having it be done where the have done we're not sure and this this is disambiguated at very least but solve our roles of API I use you wouldn't be able to to easily reuse scopes here the so other some drawbacks to what we went on specifically if you're outside and in scope if you just sort of constructing the war and ad-hoc in controller it's a little bit more verbose because you have to repeat the class names of might look something like this it's even worse if you're not using in scope this doing of really 1 of the point where this gets a little bit funky on but in the days for encouraging is for you to not have these ad-hoc 1 of queries were were were encouraging people to move things in the scopes to give them a name to put them somewhere worth of the reuse and thought about and I'm I'm OK with that so now it's about something completely different show of hands how many of you do this set your daddy's URL to your production data and then drop the database anyone knew that what happens all the time right but nobody actually get action does happen all the time this is a very common mistake that I've seen many reports of I I do know some people in this room have done if I have I have a wealth of so basically usually the way it happens is they will have used tool to pull all the environment variables down from their production server to can get environments up and if they're on something like a rope group that likely includes database URL and if they forgot onset and then they run the tests we enter using capybara can't borrow with the server that runs no threads you use database cleaner to believe everything at the end of each test and while you just got rid of your your your production this and so we decided that it was time that like this was happening frequently enough that we decide it's time to do something about it so this was the 1st guy is removed database your we run on Davis I am all through your being so if you wanted to use swirl for production you can just do that and specify that I want to that's how you want to consider but when I see that what I'm actually saying is outlets breakthrough Q and the entire workflow so so Richard Xin actually is that was the champion of this feature here everybody together and we had a little meaning and to try and think of alternate solutions so this article a lot of time and effort into fixing this particular problem in a way that didn't affect is such a stroke of anything that wants to be kind of contain a container infrastructure to sort of work out of the box on what we're doing is a little bit more complex so those are we have now is a but in addition to schema migrations which is the table we create fully automatically we now have a general-purpose metadata table just as 2 columns Archean value and right now the only entry or find and there is also the name of the environment that the last action was run against so you make you will probably not ever see this time when approach rails fight if you try and do something with your database before running a migration and that's and the destructive you'll get a an error saying hey we actually don't know what this database is just from this 1 command and then we'll know of In theory will be to the very 1st thing you do after agreeing else 5 is something that would have dropped a production database are you might still get that in some strange way but I'm basically when we're migrations we keep track of what environment we know that we're in and if we if ever trying to something that could destroy the database and the environment variables match up then we error and there's and enables the gun equals 1 the homographic that all right if you really need to this is you'll see it you see this and all of them but um you know hopefully you'll you'll never noticed this feature so this is just 1 of those minor minor things if we've done our job right you won't be aware that this is it until you actually try drop your actions but reals is now I will affect me from a surprisingly common mistake and it's 1 that was that's really catastrophic if it happens in a prepared for it so I think this is actually the features for us 5 and thank you very much richer and of and here but thank you for working on it so now let's move on to again something entirely different so migrations there is a question that
isn't terribly uh passively often so when migrations and to use it right they went and they tried like having a new developer command and not during the summer but actually just running all the migrations assess the honorably changes when that happens and you
know about so we know that this has been a problem for a while like week migrations or code that sticks around and doesn't change as not code that you test that's not coded in see if if a if a new version of rails has made a breaking change your unlikely to ever see that breakage and we have some of which were seen more and more users where there when we develop ifor for most property companies we have the development and production and has exactly 1 production and and development environments and right migration it's running production very shortly after the code was written or seeing more and more use cases where that's not the case of something like discourse or manage I Q are both places where the rails that is a product that's meant to be deployed independently they actually have a larger number of production environment an unknown number and you don't directly controlled and it's entirely possible that much more feasible there that even aside from the two-year-old migration problem if somebody if discourse has nothing really at migration and they release that person discourse and have another version of discourse and part matter of thrills 5 it's entirely possible that the person upgrading to the latest version just skip a version of discourse and is now running the migration was written from 42 against rail side and so if we change those API as ever we can cause breakage of thing is that you want to make changes of things that things have changed a lot of bad things like foreign key support I we've improved the types of indexes that we can create but we can't actually for example create a foreign key by default because that is different than what was then what was happening but when you rent migration it's over version of results so we generate new migrations with foreign key true block belongs to are now required by default but we can have we can actually change the references method in the migrations API so we generate false there and they just adds up and migrations have just on what more options there when you create an and and those are those are some more the of visible visual examples but there there's more of this some deeper changes that we would eventually like to make to the structure of the migration they can just definitely wouldn't do because they would be not just subtle changes in behavior but the really breaking so any generate of migration in rails 5 now this is what you'll see the of the square brackets after the class and it will be the version of rails generated against and if I if that's not the we'll soon for 2 there's really not much we can do for migrations older than 4 2 and basically you can be assured that migrations have ENIAC act application today as long as you're not referencing your models or any code that elsewhere in rapid changes which some people do and it's usually not a good idea as long as you're just writing a migrations through migrations API i the migration that's not today will continue to work exactly as they do until the end of time and that means that this feature isn't free because we are now dedicated to maintain every version of the migrations API codebase until the end of time so that means that at fixing bugs in the heart we'll have to think about how it how this affects older versions of migration again if effects and all our we will have to improve our change our test suite to run more and more and more versions of migrations and a larger surface area does mean more blogs are 1 things that people forget is that open source maintainers aren't magic we don't have an infinite amount time we don't match we produce coders free of bugs so more complex a feature Academy had is the more prone it's going to be 2 2 books I but I do think that this is a case where this does have a large means burden we might in the future decide that we're going to only support and migrations up to a certain number versions that can at that point I probably give commands to help squash all the migration then migrate into the new to the new flat but for now the plan is we're going just maintain every version migrations API until the end of time which does mean that we're still going to be pretty rigorous about whether or not we think a breaking change migrations API is worth that I just because it does so at that means birth but there's a lot of use cases where this is actually of really and for feature that there's just no way to work around so I think it I think what the trade off so that a interesting little method in Africa now that most people probably haven't seen in many blog posts
so access fields tells you all the fields on an accurate object that were accessed I think small names so that works so you create a new user model and axis field Wolfram and direct because you access nothing assuming not doing anything in color and then if you called that thing skilled returns a similar the so you might be asking why no so 1 of the patterns I notice is that we never call selects a mustard doing some weird calculation if if were rendering of you and only ever shows a list of users names we still selects all however many columns and knowledgeable accuse elected approach 100 and there is a performance cost there among the big reasons is that people out of reveals say like I don't want to have to look at my views and figure out what field and accessing and have to maintain so I figured I figure out if we can find it is easier so the work long-sought envisioning here is that you know that again the control or action boxes fields take a copy paste into select band of selecting less data if you change your view we give you an exception trend access a field that didn't that that you can select and so the change of unique this exception also delete the cops select and Colossus fields back and you do it over again and you will have to ever maintain it we can sort of tell you at the time I was sort of envisioning a a bullet like jam that you could run the background and whenever you would Africa models will give you a warning if you have if you were using say less than 50 per cent of the field the selected cassettes indication he might actually have serious performance benefit of but I forgot about that until the last time I give this talk so it hasn't been done yet but hope and actually it bar the reason I did this was because it's that it's an experiment but was view the trivial implement as part of the factor endowments that she's again this is sort of where casting happens I and a society instance variable with the result of it and so I can then say if something's then accessed those I just look to see the instance variables defined there are no it's I think it's a neat little feature I I was really pleased the fact that it was so easy to implement of made me feel much better about the think about the architecture we ended up with inactive record after the typecasting changes so let's so
subtle now it's a little easier to so that Booleans everybody's favorite so never questioned the eyes some listening affronted false now I know I know it's nobody's I like it the simple there's 2 values so it is not so it's a good was can we have a blooming field and we objects or going to assign the result of whether or not this rejects matches the string to the Boolean field Anastasis it's very clear that the string will match the rejects they think about love value the fields in the not the data yeah I know I wonder politicking false why would I be asking if the answer wasn't falls behind so from my from Albert these names just the tree this layer looks like on the actions of this question and I the could actually like this is using this
wittily operator in 7 matching operator and and the reason I switch to the match data is because this 1 has a different copy and all that in a minute I but basically the the problem is in for 2 and more Sicilian in for 1 of the way the Boolean-type type works as we have this constant and it has all the true values and includes the string T which is what the error the string 1 rather which is what going get from the Form Builder 1 because 1 extreme 1 another integer 1 string T I'm assuming because that's what PostScript has an at some point that went through untouched but that hasn't been true for a while on the street on which works in sequel light and I think Oracle and a bunch of others and everything that's not in this array is assumed to be false I and the problem is so . matches returned the match the object match not near a true value so it's false other squiggly operator here returns the index of the of the the character that match the right and so interestingly enough if if the 2nd character in the string happened to be the 1 that matched than others so the current 1 and it would be true of every other every other values false hum and so we actually willing to overlook interest how long we just get Boolean yes or no does this rejects match from that this is the answer triple equals is the only method on rejects or string that did as a billion and so this lecture the full list of the values for which which were true the in rails for 1 and earlier on the some have review works but we don't have the employee of the known set of true and everything else is false that's all right I mean how it how we're working now is not our works by the but were closer so I'm really knows falsely false assaulted everything else is truth so the filtering using and boys values which of course and erotically this doesn't actually quite fixed that original squiggly operator problem is the 1st character of the string matches it's then return 0 and zeros on that list so this could capture there for a year but wouldn't save for 1 because you would have gotten deprecation warning in 4 2 your affected by this so don't don't I'm sure some if anything like 0 my god were signing rejects matches to the end field of our user model and now everybody can be an admin on how the nodes here if you have a images value for 2 you would have gotten deprecation on it also this is another 1 where it's my area you will probably never notice it and hopefully hopefully now you will definitely never notice that the nodes of the behavior before was because it was doing something really confusing really wrong and I'm glad we're doing more the anyway so that those are the stories I was hoping to share the guys but I would like to why have kept volumes pitch my new or an interest it's called diesel i've stickers Lexicus confined afterwards this is our website big thank you shall provide a sponsor me to work on open-source time and they pay for me to come to all the conferences and speak to all of you out if you like to work with profile and I both chapter 5 please let us know thank you very much element to
questions yeah few
yes you the other question was if you can chain not worth you can chain not wars and you can combine it with in as many in the cells don't we're not we're not where you been chain and the president's will be basically exactly to us the question was has them and talk about any explicit I here about war no I'm not sure that that is something that we should be added but there's an interesting thought and I will I would be just look at how it affects the again on the no there has not been explicit such as the with reason intervals about yes knows no and if if you're a fan of your column is not known and unknown will so frightened answer is not and so little book of the question was is there planned for access field on relation and no because like the implantation we just the if the 1st element would be calling on men and I can't really know if you're going to use every model and collection the same way and this is very easy for you to do that 1st on-axis fields if that's what you if that's the need of the question was what is the metadata table I mentioned cold and I had a pair if you are aware that my client I believe I remembered exactly my believe it is double underscore Active Record underscore metadata so I think it's pretty unlikely that you will be outlined the if you do have that I'm interested how you got like the model name to match up with that of via of so I I don't expect that all flights they but that was in some problem that's why the double underscore from and so the question was common practice is to pull them a production database locally and how does that interact with our protection against destructive actions and so tools that are meant to use with rails for that sort of thing should be it should just update 2 x to the metadata table that similarly to how I would imagine that base analogous they would increase their migrations and basically what will happen is you will get that error message I'll say you are we think that this is our production and your friend runs in development these run this command if you got updated using this error so you just need to run that after after you up after people that of course was dropped metadata also there yes as long as you run a migration before you try and do something destructive otherwise you get the same error because we don't know the question is how square records on migrations implemented with we doesn't the US were expected on that on the class object itself and we return a new anonymous class it works very similar to if you use of if you use any chance that have like I included there is a gem that influence the compared methods for you which I don't remember the name of it but OK equalizer and the way it works is do I include equalizer that new food Bob as and it works it's worth the somewhat with that just added a new anonymous class and the question was driving ideas for real 6 and things that are going to require major differences in the assuming specifically mean differences in the connection after but yeah but the main thing I wanna do is get the of the third-party connection adapters onto public API so that way there is more stability unfortunately that change will have to be a breaking change but I I'm hoping to basically reimplement a lot of the connection actors to be on top of an API that we think can be more stable in the long term and then and then work with third-party adapters to get over on what the only other thing and specific with thinking of real 6 right now is moving actually take guy up to active model of fall involves breaking changes with things like dirty checking will need to go through the activity that would just work with regular and excessive which I don't think will be a huge impact but my need to explore a little more the I think very much everybody else from around here such as if I have if my


Formal Metadata

Title Rails 5 Features You Haven't Heard About
Title of Series RailsConf 2016
Part Number 60
Number of Parts 89
Author Griffin, Sean
License CC Attribution - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
DOI 10.5446/31548
Publisher Confreaks, LLC
Release Date 2016
Language English

Content Metadata

Subject Area Information technology
Abstract We've all heard about Action Cable, Turbolinks 5, and Rails::API. But Rails 5 was almost a thousand commits! They included dozens of minor features, many of which will be huge quality of life improvements even if you aren't using WebSockets or Turbolinks. This will be a deep look at several of the "minor" features of Rails 5. You won't just learn about the features, but you'll learn about why they were added, the reasoning behind them, and the difficulties of adding them from someone directly involved in many of them.


AV-Portal 3.5.0 (cb7a58240982536f976b3fae0db2d7d34ae7e46b)


  419 ms - page object