#RubyOnRails - 31 July 2018
« Back 1 day Forward 1 day »
[02:33:01]
rashkov:
Is there a way to do this: I have a Participants model. It has a participant_type field, which is set to one of: "student", "teacher", or "volunteer". How would I do a "student has_many: teachers" relationship?
[09:05:12]
mengu:
the thing is, the app lives in a docker container, so i cannot change files and restart
[09:05:30]
mengu:
and that's because docker service starts the app again with the same image, which does not contain my change in files
[09:05:52]
mengu:
i was thinking maybe i could install byebug or some debugger and intercept in the running process
[09:07:55]
TvL2386:
not sure how much work, but maybe you can create a new image with debugging in it?
[09:08:24]
TvL2386:
also thinking: maybe you can tcpdump the communication if it's not encrypted (probably not the case)
[09:08:50]
TvL2386:
maybe you can trigger this communication from the rails console and inspect what's going on?
[09:13:05]
TvL2386:
but, I don't think that's possible (without building shell scripts using as an entrypoint)
[09:14:12]
TvL2386:
I was also thinking: Modifying the container while it's running, don't --rm it, save it as a temp new image and start it again
[10:26:41]
timwis:
Morning folks! Trying to determine which ActiveRecord relation type to use for this scenario.. my users have a "rank" column that references a ranks table. There's only 5 ranks but hundreds of users. Should my users model has_one :rank or belongs_to :rank? I definitely don't want deleting a rank to delete associated users, and would assume belongs_to does that. (the user is more important than their rank, kinda thing)
[10:41:48]
tbuehlmann:
timwis: you probably want this? `User.belongs_to :rank` and `Rank.has_many :users, dependent: :nullify`
[10:42:22]
tbuehlmann:
this will nullify users.rank (which should be users.rank_id) when a rank is deleted
[10:51:21]
timwis:
tbuehlmann: interesting; i'd have thought the nullify bit would go in the migration rather than the model
[10:53:49]
tbuehlmann:
the dependent option is used and ran in ruby-land when destroying a rank object
[11:05:32]
theRoUS:
rails 5.1 with mongoid. is there a way to have the generic model #find method search for a different field than _id, or in addition to _id? the equivalent of #find_by(:name => val) or #where('$or' => [{:name => val},{:_id => val}]) ?
[11:12:11]
Terens:
I have a column stored called eg: template. I want to return full object with compiled template , where should I put this logic ?
[11:14:32]
timwis:
Hey, one more question if you don't mind: I have a rather complex permission logic that queries the database to determine whether the subject is under the actor in a hierarchy. Does it make sense for that query to live in my User model, like a get_permissions_on_group method? I plan to use pundit to lock down my controllers, but need the logic in place first.
[11:14:50]
timwis:
I'm using the postgres ltree extension for the query, so I expect I'll need a combination of ActiveRecord and a little raw sql
[11:16:32]
tbuehlmann:
I think having User#group_permissions is okay. if the query or scope is quite big, you could outsource that into a query object or something similar and simply use it in #group_permissions
[11:22:09]
timwis:
tbuehlmann: interesting. convention is not to use a verb in the method name? i'm new to ruby; coming from JS land
[11:28:38]
tbuehlmann:
timwis: right, we don't need "get" and "set" in ruby. `user.group` is the getter and `user.group=` is the setter (if defined)
[11:46:15]
timwis:
tbuehlmann: I might be diving into the deep end of ActiveRecord here a bit too early... I've got this to work in PostgreSQL - would you recommend I just write a raw query, or try to use scopes or something to replicate this in ActiveRecord? https://stackoverflow.com/questions/47360881/how-can-i-determine-permissions-in-a-hierarchical-organization
[11:48:21]
timwis:
I've got all my models created, and fixtures, and even a couple unit tests ready to go. Just need to write the actual query method. It just happens to be rather complex (and the SO question there is a little simplified to be more concise if you can believe that..)
[11:53:40]
zmo:
I'm writing tests for a controller using rspec, and I'm trying to check the content of a response. I get `expect(response).to render_template("new")` but when I do `expect(response).to have_content("sh!t that should be there")` it fails
[11:54:04]
zmo:
so I'd like to introspect the HTML that has been rendered, but I don't find what method gives that to me
[12:17:01]
zmo:
or, is there a way I can test the instance variables I setup in the controller that I give to the view ?
[12:18:24]
cmartin:
Morning all. In Rails 5.2 has anyone noticed a difference in delete_if? Have a method that accepts an ActiveRelation that I then run a .to_a.delete_if on. I've confirmed that if I run the delete if the array is modified, however the parent object that had the .to_a ran on it is not.
[12:20:52]
zmo:
TvL2386 - yeah, is there a _clean_ way I can try asserts on the controller's instance variables?
[12:22:12]
nzst:
cmartin: to_a doesn't modify the parent object, it returns the parent object as an array. So when you chain the method #delete_if it is operating on the return object
[12:23:40]
cmartin:
Ahh, that makes complete sense. This was an upgrade from 4.2 so that behavior does act differently (maybe a bug in 4.2)
[12:23:56]
TvL2386:
zmo: I'm no guru on the testing subject, but I think you should not test the underlying code. So fetching instance variables from objects and/or inspecting them should not be done. (correct me if I'm wrong!)
[12:24:58]
zmo:
well, I went that way because I don't get why I got no data from the response's body
[13:08:12]
TvL2386:
zmo: found it! This is what you need, but please read the _comment_ : https://gist.github.com/eliotsykes/5b71277b0813fbc0df56#file-rspec_cheatsheet_controller_spec-rb-L18
[13:09:57]
TvL2386:
zmo: https://relishapp.com/rspec/rspec-rails/v/3-1/docs/controller-specs/render-views
[13:12:58]
theRoUS:
rails 5.1 with mongoid. is there a way to have the generic model #find method search for a different field than _id, or in addition to _id? the equivalent of #find_by(:name => val) or #where('$or' => [{:name => val},{:_id => val}]) ?
[18:57:43]
cjohnson:
In rails, I've got Pharmacy, PharmacyOption, and Option. PharmacyOption belongs_to Option and Pharmacy
[18:58:09]
cjohnson:
I want to generate a form where I iterate every Option, and for each Option, show a fields_for form section for the relevant PharmacyOption if one exists
[19:36:39]
Terens:
I have a model called eg: XTemplate with several attributes. It has an jsonb attribute called template that has several keys. Only a subset keys should be rendered using a templating engine filling existing placeholders.
[22:00:39]
timwis:
Hey folks, I'm constructing a complex permissions check query for the current user. It involves a couple nested joins with conditions. Should it be a class method on User, or an instance method? (Since the current user will already be loaded)
[22:36:15]
timwis:
newton: that's what I thought but won't I need to use .joins() ? And that isn't available on instances right?
[22:36:58]
timwis:
I'm new to rails (coming from node) and haven't been able to find a good example of something like this
[22:39:28]
newton:
you could do it either way really then. you could implement it on the user and have it just call self.class.xyz to handle the query, or make it a class method you pass the user to
[22:43:43]
timwis:
It seems like a relatively common issue though. Any chance there's a name for it I can Google or find an example of?
[22:53:50]
newton:
not sure if there is a name for the problem - I'd assume stuff like this is pretty opinionated. If the logic is big or complex, you might consider turning it into a service (another class) that handles it