#RubyOnRails - 13 April 2017
« Back 1 day Forward 1 day »
[03:19:05] DoctorMonocular: ok kids, naming question: I have a node and graph data structure. I have a method that returns all nodes within N steps of the given node. What should I name this method?
[05:38:46] bios__: Hey *.. I've got a validation on a model, but I'm not sure how I can access the error messages in my view to show the user. Any help would be great
[05:39:25] bios: my validation is as follows: validates_presence_of :event_end_date, :if => :is_multi_day?, :message => "Please enter an end date if the event is multi-day."
[06:01:26] sevenseacat: a bit confused about your logs saying it's a JS request, but your controller not supporting that
[06:03:33] sevenseacat: https://gist.github.com/mikesaltsman/f93d745310900cf00611e5ee8ff9255f#file-gistfile1-txt-L2
[06:07:39] bios: Ok.. well, I guess what I was looking for was that I am understanding where the errors should be showing up, correctly
[06:08:27] sevenseacat: https://gist.github.com/mikesaltsman/ccc4c39fb0abf861cb1013df4d404be9#file-gistfile1-txt-L7 whats this do?
[06:10:02] bios: I put it in the comment.. it's a method in the model that I pulled out before posting to gist
[06:10:37] bios: https://gist.github.com/mikesaltsman/ccc4c39fb0abf861cb1013df4d404be9#file-gistfile1-txt-L7
[06:10:58] sevenseacat: so if that returns false, the save won't go ahead - that can cause a rollback
[06:11:37] bios: ok.. that makes sense.. but why would that fail only when validations fail, I wonder
[06:13:04] bios: sorry.. I'm not 100% confident I understand.. so a lot of my statements are actually questions :]
[06:14:45] dminuoso: bios: Validations are like poor mans constraints, except they don't constrain anything. But they seamlessly integrate into form builders. :-)
[06:16:42] bios: I'm going to try to follow the timezone logic and see if I can see where it would fail
[06:23:57] bios: yeah, the error is showing correctly if I output the error from the update action, but it's not in my view
[06:24:45] bios: I wonder if I'm making a new @event object and that's killing my errors before I render the view?
[07:47:14] tbuehlmann: dminuoso: now it's validations! what's next? callbacks? instance variable sharing in views? directory structure? oh wait, nevermind
[08:03:24] workerbeetwo: Hi. Is there a way to reduce the rails server output to databse interactions?
[08:03:57] sevenseacat: I think you'd have better luck using instrumentation to log all of the queries to a different file
[08:27:05] universa1: dminuoso: you can always have monads: https://codon.com/refactoring-ruby-with-monads *cough*
[08:28:03] dminuoso: universa1: I actually have Maybe monads floating around in a project of mine.. :-)
[13:14:32] toomus: Maybe someone will know why in RSpec tests with type request, controller doesn't see anything I set to session
[13:17:51] toomus: I mean, I save something to session in test, just before request, and in controller I expect to receive this value in session, but session doesn't contain it
[13:19:22] toomus: I tried: session[:v] = 'val', request.session[:v] = 'val', @request.session[:v] = 'val'
[13:28:52] universa1: toomus: your update_request let is somewhat counterintuitive and probably won't work with multiple it blocks
[13:29:26] universa1: and then the result of that is saved, so on subsequent calls, it doesn't get reevaluated iirc.
[13:29:47] universa1: so you probably want to return a lambda or alike from that and do, update_request.call
[13:31:03] universa1: and request specs are "integration tests" if iirc also, so directly setting the session does not work.
[13:32:23] toomus: universa1: so how to setup session if I need signed in user, and I set this session value in sessions controller when user logs in?
[13:32:43] syndikate: Hello people, how do I updated an enum value from rails console? I tried some_instance.update! status: 2, it is not being reflected although the query runs fine
[13:33:05] universa1: toomus: for example: https://makandracards.com/makandra/37161-rspec-devise-how-to-sign-in-users-in-request-specs
[13:35:21] tbuehlmann: you could also just stub the current_user method, returning the user you need
[13:43:26] universa1: tbuehlmann: kinda hard in a request spec (integration test), since you have no actual instance of the controller, and stubbing any instance... ;)
[13:51:09] tbuehlmann: I'm okay with allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user)
[14:33:11] rashkov: is there a way to broadcast some data to a user immediately after the user subscribes to an action cable channel? I try to call ActionCable.server.broadcast from the channel's subscribed method but it doesn't reach the user, I think because they're not fully subscribed yet. I guess I could have the user hit a traditional controller endpoint after subscribing, but I'm wondering if there's an action
[15:28:29] xco: hello, i want some advice. i want to use rails of course but what’d you recommend for me to use if i wanted to create an app that, given a screen in the browser with a polygon, a user clicks and a response from the backend tells whether or not the point clicked is in the polygon on the screen?
[18:19:04] shinobi_: Does rails drop tables and then recreate and migrate upon every test run, or how does it handle the data?
[18:20:55] cpruitt: shinobi_: The tables are blank & rails runs everthing in transactions that are rolled back.
[18:26:13] Darmani: Hey guys, so I have this form that dynamically loads a partial onto my page. But I need to add an event listener to an element on that partial.
[19:51:10] jstrong: Hey all. Having some issues setting up a one to one polymorphic association. See the following gist for information in terms of how I’ve set it up: https://gist.github.com/laspluviosillas/95c66a2976cd3fa52688c9e5a2f69e7b
[19:51:42] jstrong: Any suggestions as to what I’m missing? The ‘has_one’ assocaition seems to be insisting that the foreign key be in the table that defines the has_one relationship
[19:52:28] jstrong: ‘has_many :notification_preferences’ works (since it expects FK to be defined in the other table), but I don’t want to make it possible to have multiple notification preferences per task, jsut one
[20:05:20] tubbo: jstrong: belongs_to will try to turn the first argument into a class name, and since there's no `Notifee` class, it will most likely not know how to rehydrate the object from an ID
[20:05:57] jstrong: What happens is that I can do, for example, task.notification_preference = blah no problem
[20:06:11] jstrong: But if I try to do Task.find_or_initialize_by(notification_preference: blah) then it tries to look for notifee_id
[20:06:32] jstrong: Gives me the impression that the find_or_initialize_by in the particular version of Rails I’m working with isn’t properly coded to work with has_one polymorphic associations. Could be jsut a bug.
[20:15:34] tubbo: jstrong: if you're generally creating notification_preferences that way (by way of a Task), the _id should really "live" on Task
[20:16:34] tubbo: jstrong: if you don't have a Task saved yet, then the NotificationPreference can't be saved, because it needs a notifee_id and your Task doesn't have one yet.
[20:17:21] jstrong: tubbo: That’s not the issue really. The issue is find_or_initialize_by expects all has_ones to define the foreign key in the table that contains the has_one (so it looks for notifee_id on Task)
[20:17:53] tubbo: jstrong: right, hence why you can't write the association this way. you're starting with a Task
[20:18:29] tubbo: if you did NotificationPreference.find_or_initialize_by(task: task) that would work
[20:19:27] jstrong: (I’m “lucky” in this instance because I don’t actually need notification_preference as part of my find or initialize by. It’s not one of the restricting conditions)
[20:20:21] tubbo: jstrong: like i said before, you can't go from task -> notification_preference because you haven't saved the task yet and notification_preference needs an ID of its notifee in order to proceed.
[20:20:54] tubbo: the way people typically solve an issue like this is by leveraging mass-assignment and accepts_nested_attributes_for
[20:21:38] jstrong: tubbo: Here’s the end code I’m going with https://gist.github.com/laspluviosillas/7046c099920242c7917ef727d4bb1bad
[20:22:27] jstrong: Although the only issue here is the find_or_initialize_by when passed in the initialized task is going to try to look for NotificationPreferences with notifee_id nil! Which is an unnecessary query. Perhaps just better to go “old school” and fix the issue with if/else logic.
[20:22:45] jstrong: Not only unnecessary query but could potentially cause some major bug if somehow a NotificationPreference with notifee_id: nil ended up creeping its way into the database.
[20:22:54] tubbo: jstrong: cool. you can also do this i think: https://gist.github.com/tubbo/009e77ac61f872ff363978a9301cbba0
[20:23:18] matthewd: I'm not sure I see why the original thing "shouldn't" work.. but maybe I'm just tired
[20:23:51] tubbo: matthewd: wouldn't it freak out if Task hasn't been saved yet? Task#id is nil, so NotificationPreference can't set notifee_id, and belongs_to validation fails
[20:23:57] jstrong: tubbo: and I assume that that find or initialize by is not going to try to use the nested attributes in the query that does the find? it’s smart enough to figure out that the nested attributes represent an object that cannot exist.
[20:24:59] matthewd: tubbo: But it's not setting notifee_id, it's setting notifee. It's certainly permitted to build multiple not-yet-saved objects together before saving them.
[20:25:06] tubbo: there are hacks to get it to destroy objects like passing _delete: true IIRC (the way you'd do with a nested form)
[20:25:31] jstrong: matthewd: right, that’s what i thought. but I legit think find_or_initialize_by is just not smart enough to figure all that machinery out.
[20:25:55] tubbo: matthewd: i'm not saying it's NOT a bug haha...but i don't remember find_or_initialize_by ever working the way you're describing right now
[20:26:13] matthewd: jstrong: https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
[20:27:16] jstrong: Well what tubbo was saying is that find_or_initialize_by IS smart enought o realize that parameters that are nested attributes cannot possibly be what we want to “find” so it just ignores those when constructing the find query?
[20:28:27] tubbo: matthewd: [OT] what's going on with AS::Reloader? i noticed that it got refactored out into implementing AS::Executor, but i can't find any of the docs related to the Reloader API anymore.
[20:28:33] matthewd: tubbo: Nope; it'll try to do the "find" part, and fail before it ever gets around to the initialize
[20:29:00] tubbo: it would really help my company because we rely heavily on rails autoloading, and decorating (changing code at runtime) objects
[20:30:16] matthewd: You [hopefully] mostly want the much-overdue-for-a-revisit-and-merge https://github.com/rails/rails/pull/27494
[20:32:58] jstrong: Other question. Let’s say you have four parameters you’re trying to mass assign. Three of them are what you what to “find or initialize by” and the fourth is one you just want to set (in other words you don’t want it to be part of the find query). What’s the convention for doing this?
[20:34:14] matthewd: Err... create_with if you only want to set it on new objects... assign_attributes if you always want to set it
[20:48:15] mlt-: How would I create 2 sibling tags in custom input for simple_form? I know I can wrap both siblings into yet another tag, but can't quite understand on doing it simple
[20:54:57] mlt-: tubbo: actually I see now in wiki they use SafeBuffer and write stuff in there https://github.com/plataformatec/simple_form/wiki/Adding-custom-input-components
[20:56:38] mlt-: I was confused as many examples use do .. end block and use template.concat in there
[20:57:00] tubbo: just learned about https://github.com/andreapavoni/simple_form_fancy_uploads from the page you linked
[20:58:47] tubbo: mlt-: SafeBuffer is the object user under the hood when you call `.html_safe` or `raw()`
[20:59:16] tubbo: raw means "take the string i pass in as an arg and wrap it with a SafeBuffer", and html_safe means "take the string this method was called on and return a new SafeBuffer with the contents of that string"
[20:59:31] mlt-: tubbo: my only "problem" I feel like it could be 2 lines functions instead of 3 :-)
[20:59:57] tubbo: concat is "take all arguments given to the function, concatenate them together, and wrap the whole thing in a SafeBuffer"
[21:01:02] tubbo: (i'm actually not sure if SimpleForm::Inputs::Base) includes the ActionView helpers
[21:02:25] tubbo: mlt-: oh sorry, no i am not... http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-concat
[21:07:41] mlt-: what is also not trivial to me is whether I can use child tag instead of a sibling. But in this case if I use template.content_tag with block, block takes over actual content :(
[23:07:59] CGibsonmm: undefined local variable or method `post' for #<RSpec::ExampleGroups::CanViewIndividulPosts:0x007ffb137468a8
[23:08:14] CGibsonmm: I have a question about a feature test. line in question= "expect(page.current_path).to eq(post_path(post))" rspec is returning undefined local variable or method `post' for #<RSpec::ExampleGroups::CanViewIndividulPosts:0x007ffb137468a8
[23:39:29] patrick99e99: Hey everyone, I am just updating my app to rails 4.2, and am finding that my application.scss.erb is not getting preprocessed? I tried putting a debugger statement in the file, and nothing happens.. If I switch back to rails 4.1, then I hit the debugger... Any ideas why this file would not be getting executed as erbb ?
[23:46:05] patrick99e99: weird.. it's like the file only gets compiled occasionally...... ahh must be something going in tmp/cache for capybara..