#RubyOnRails - 21 October 2017
« Back 1 day Forward 1 day »
[05:31:15] timdotrb: Anyone familiar with Highcharts? I’m trying to get the first and last values after changing the zoom to calculate the difference between them and am getting strange values
[11:24:28] erts: Hi everybody! Beginner question here: I see that, generated by the scaffolding, when a form doesn't pass the validation on submit, it renders the new view through the create controller method. This is fine, it gives me access to the form errors and all the rest, but the URL doesn't change, so if the user refreshes the page they'll get an error or some other result on the browser. I know this is intended, because the user is supposed to just fill the
[11:24:29] erts: form again (correctly) and hit submit, but is there any workaround to protect ourselves from this behaviour? Thanks!
[11:27:43] matthewd: erts: "an error or some other result" sounds like a broken browser. You can redirect back to the form if you like, but you'll have to pass along whatever's needed to reconstruct the validation failure etc.
[11:29:03] erts: matthewd: I was referring that rails gives you an error if you don't have an index method, in my case, if I refresh it takes me to the index when I was meaning to refresh the "new" form
[11:29:34] erts: having an index method there is no error, it's just annoying, if you don't have an index method you would get an error saying that you can't request to that URL using GET
[11:30:26] erts: could you give me a good example to reconstruct the validation failure errors? I've been trying a few things but it's quite messy. For some reason I feel like this is very hacky and there should be another way to solve it
[11:31:35] erts: I guess I'd be happy enough rendering the new view from the create method as usual, but updating the URL in the browser, so the refresh scenario doesn't happen
[11:39:12] matthewd: You submit the form, it POSTs to /articles (routes to ArticlesController#create), which shows the validation failure. If you then hit refresh, the browser should resubmit the POST, again routed to #create.
[11:40:42] erts: yeah in my case in this scenario, the browser doesn't resubmit and it takes me to the index action
[15:34:32] reves3: Hi there, I am new around here. I guess this question has been asked a tousand times, I learnt RoR some years ago and developed an app content managment system for a non-profit organization. Since all around RoR was so new to me, I didnt develop any tests around the app. Now I have a little free time again and I would like to upgrade the app or perhaps create from the beginning. The current version is
[15:36:39] matthewd: reves3: If it's smallish, I think I'd consider recreating it -- possibly copying and adapting parts of the existing implementation in the process
[15:38:52] matthewd: If it's large and complicated, I'd more likely try to add tests to the existing app, then upgrade afterwards -- it's harder to be confident about test coverage when adding them retrospectively, but it avoids a situation where you're working on a new/undeployed app for a long time
[15:41:33] reves3: so i would say it is a small app, here is the repo https://gitlab.com/reves/CGDV-CMS
[15:42:19] reves3: I have it running and until now we havent had any problem, but i really guess it needs and upgrade
[15:54:09] matthewd: reves3: https://gitlab.com/reves/CGDV-CMS/blob/master/config/initializers/secret_token.rb#L7
[16:00:52] matthewd: The multilingual mixture of names seems like it would be unpleasant to work with
[16:01:16] matthewd: (but I acknowledge that's much easier to say when I've never really had to consider an alternative)
[16:21:34] matthewd: reves3: So, from a quick read-through, yes I think I would rewrite -- but that partly depends on what you think of the current code quality level
[16:21:52] matthewd: reves3: .. and also how important quality is, I guess: if it's hardly ever touched, maybe it doesn't matter
[16:28:03] reves3: matthewd: Thanks a lot, well I am the only developer and I guess it will continue to be so, so the code is hardly touched. But now and then I try to upgrade it to the latest version of rails, but sometime I have cannot do it that often. So now that I read the code I find it hard to maintain :P So I guess I would rewrite it
[18:45:44] crova: greetings everyone! I'm trying to store an object extracted from an API call. I'm not sure how to build the controller to store it, my currently code returns me the error found on this gist: https://gist.github.com/crova/95ac81258d1079bbdfffdb3877a0bb3f Anyone up do help a noob out? Cheers
[18:50:45] crova: matthewd, well, a hint on how to save each value of camp_data_name, camp_data_comission and camp_data_subid into the database. What I came up with tells me that I need to pass up a hash as an argument when assigning attributes. I looked into this error but did not manage to make sense of what I found on stackoverflow
[18:56:07] crova: I isolated the 'names', 'subids' and 'comissions' on different arrays and was trying to iterate through the camp_data_name array to save each value as "camp_name"
[18:59:40] matthewd: Okay, different question.. how close do you think the current code is to working?
[19:15:36] matthewd: The plucking seems odd to me. The data is arriving with the various values grouped together for each record, but you're flattening that out into a series of disconnected lists.
[19:17:53] crova: I thought it would be easier to isolate them before storing, but finally it didn't help anything (mostly because I'm not sure on how to do it). So I guess I could use the object before all the plucks
[19:23:30] crova: matthewd, I updated the gist with the full API response, before the digging, I just wanted to get rid of the "request" and other unnecessary information
[19:24:53] crova: but, It is working with "Ybox.create(:camp_name => x['name'], :camp_total_comission => x['approved_comission'], :camp_subid => x['affiliate_info1'])"
[19:25:52] crova: matthewd, truth is, I will make a lot of weird thing due to lack of basis. I will start to read the well grounded rubyist to get some knowledge on how stuff works and how to build proper code
[19:26:21] crova: so far I just watched a few tutos and started working on something I needed, but if you check my app is a freaking mess
[19:29:24] matthewd: Yep, that's fair enough. TBH this looks surprising to me partly just because dig and pluck are both quite new -- so they aren't what I think of as 'default' approaches
[19:32:55] crova: I stumbled upon those while trying to store another API response to the database when I was starting and I guess it just sticked in my head
[20:03:55] mynameisdebian: I am building a database in MySQL that I would possibly like to translate to Rails later. I am reading information here on "Rails-friendly SQL" https://www.tutorialspoint.com/ruby-on-rails/rails-active-records.htm . It says "Given entity x and entity y, if entity y belongs to entity x, then table y has a field called x_id." What does this mean when I have a many-to-many relationship? For example, I have a table "Games"
[20:03:55] mynameisdebian: with a child table "Categories". A category can have many games, and a game can have many categories. So I have an intermediate table, "games_has_categories". The PK field of Games is "id" and the PK field of "Categories" is id. Will the fields in the "games_has_categories" table be called "categories_id" and "games_id"?
[20:05:22] matthewd: mynameisdebian: The default name for the table would be categories_games, and the FKs are category_id and game_id
[20:06:29] mynameisdebian: matthewd: does "categories_games" imply that categories is the child of games?
[20:06:37] matthewd: The x & y in the sentence you quote are misleading too... the table `ys` has a column `x_id`
[20:08:01] matthewd: No; it's a relationship of equality, so the ordering of the names is arbitrary -- it just has to be consistent when coming from both directions. So the two model names are sorted alphabetically.
[20:09:51] matthewd: If you want Rails to be able to guess the table name, they're sorted alphabetically. If you want to specify the table name manually, you can call it anything you like.
[20:12:02] mynameisdebian: matthewd: BTW, wouldn't the FKs be called "categories_id" and "games_id" (in the plural, as with the original tables)?
[20:13:14] matthewd: If it's an FK, it'll always be category_id -- it's an integer, so it can only be pointing to one category
[20:14:23] mynameisdebian: the link I gave says each table should be named in the plural. So if I have two tables named in the plural and I establish a many-to-many relationship between them, won't Rails just insert the underscore? Or are you saying it intelligently reduces "categories" to "category"?
[20:15:45] mynameisdebian: I just want to clarify, you're saying it's embedded in rails for it to know that "categories" is the plural of "category"?
[20:16:57] mynameisdebian: what if a table were called reveries, would Rails then incorrectly reduce it to "revery" (not a word), or does it have a large dictionary built-in to know the difference?
[20:19:49] matthewd: There's a short list of rules that attempt to get decent coverage (https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflections.rb), then per-application exceptions for anything they miss
[20:20:59] matthewd: Per the comment there, the rules are basically stuck now, even if we realize one is generalizing an exceptional case, or something
[20:22:17] mynameisdebian: I think it's pretty cool, it's just so unusual for a DB of any kind to do something like that
[20:25:48] matthewd: Yeah, I'd agree with that -- it's doing a more complex thing (with risk that it'll get it wrong), just for a small improvement in cosmetics/ergonomics
[20:26:56] mynameisdebian: yeah right, now that I think about it it clearly adds nothing, when they could have just kept the plural name
[20:28:56] matthewd: Especially when you move past the actual DB schema, and into the models: you really do want the class to be called Category, not Categories, for example
[20:39:54] mynameisdebian: I think because I'm just learning Rails I don't completely understand the benefits
[20:45:42] mynameisdebian: Say I have a table that refers to itself on a single primary key (Games has many other Games). Since I can't have two fields on "game_id" on the join table, is there a default for the names, or do I have to use an exception in Rails?
[22:20:33] Nuck: What would you guys suggest for grouping actions to be done at the end of a request or background worker? So, like, if I have to sync a set of updates to an external API and I want to batch them up
[22:21:34] Nuck: So maybe I call `do_thing` 5 times during a request in various after_commit hooks, but I want them to run at the end of the request as some big bulk hit to reduce overhead (there's noticeable lag on each request)
[22:22:18] Nuck: I know Chewy does this for elasticsearch, but I can't figure out quite how it's doing it. I was thinking of using Thread/Fiber-local variables but that seems prone to, uh, *issues*
[22:24:17] Nuck: Sending the batch is easy, I can do it in a controller lifecycle hook or rack middleware, but building the batch seems trickier
[22:34:22] stevednd: I'm getting a load error when trying to require ruby-bitly into my rails app. I get the message `Bitly is not a class` which it's not. It subclasses OpenStruct. It works fine on its own outside of rails. Is there some way to work around this?
[22:56:55] gr33nw00d: In many cases the end result is the same. What is the code guideline/best practice for using instance vs regular variables?
[23:05:13] Skull0Inc: gr33nw00d good question! I came across this issue myself. What I realized is that its best to use the @var for allowing the view access to that resource and just keep it as plain var for just use by the controller not meant to be shared
[23:05:53] gr33nw00d: I found something similar @Skull0Inc - I will default to the smallest scope that works (local) and use instance vars when needed
[23:06:18] Skull0Inc: gr33nw00d: However I'm kinda new myself and would defer to other more knowledgable opinions.
[23:09:38] Skull0Inc: gr33nw00d nice! just about 6 months now. Love it! Just recently learn authentication (custom) / authorization (before actions- custom) and Pundit.
[23:10:10] Skull0Inc: I had that issue with pundit where I didn't know whether to use just var or @var because they have something called @user and @record.
[23:10:36] gr33nw00d: but if you don't need the value to be shared between methods you should use a local var
[23:11:28] Skull0Inc: they actually have some pre-requisites that require the use of defining a pundit_user methof
[23:12:48] Skull0Inc: made it local vars I think, unless I had to display something from user on the page.
[23:13:31] Skull0Inc: Love how access can be done just like that...took me a few days to 'get it' though.
[23:13:48] gr33nw00d: My application_policy.rb provides access to current_user via the initialize(user, record) method
[23:14:59] gr33nw00d: Then in all the sub-policies you can just use @user (referring to the current user)
[23:15:11] Skull0Inc: I ended up creating a method pundit_user which is called by Pundit, every action for defined controller.
[23:18:40] Skull0Inc: such as going to users/11/edit. when going there it would call pundit_user set the @user in the policy and check against the passed in record in this case user/11
[23:19:40] Skull0Inc: Sorry, went off on a tangent there. I just realized that I'd like to contribute to things I can help with.