|
I'm having a little trouble finding the best way to handle my associations and callbacks. I have a Parent and a Child class. Parent has a field called master_count, Child has a count field. Each child of the parent needs to be assigned a portion of the master_count. Ex. Parent has a master_count of 10 ChildA gets a count of 6 ChildB gets a count of 4 If a child is created/updated, the parent needs to recalculate which children get which slices of the pie. They're currently all in a nested attribute form. Ideally, I would like all of the Children to be updated and then Parent calls it's assign_count method. However, it looks like Parent's after_save callbacks run before the Children get saved, so I can't call assign_counts there. I thought about giving Child's association a touch: true, but that will cause an infinite loop. I feel like I'm missing something simple.
|
# ¿ Oct 22, 2014 13:52 |
|
|
# ¿ Apr 27, 2024 11:40 |
|
Cocoa Crispies posted:In a situation like this, I'd be really tempted to move the behavior of what you're actually working with out to a new class, even if it isn't something actually persisted into the database. This makes sense. The allocation code is cluttering up Parent, and it'll be much easier to test and modify outside on its own. KoRMaK posted:What order are these things getting saved as? Why are you saving the parent first instead of the children? I'd put the after_save on the children to call the Parent. I'm using a form for the parent, which has nested fields for each of the children. Rails processes and saves the parent before it even looks at the children, which makes sense for most cases. Ideally I'd be able to process all the data on the forms for the Children and the Parent, save them, and then call the PizzaAllocator, which will process the data and save them again. I'm just not sure how to make this happen. My main problem now is that I don't know where to put the calls to PizzaAllocator. I've learned that I can use update_column instead of save in the Allocator in order to avoid triggering after_save callbacks. I can put my calls to the Allocator in after_saves on the parent and the children, but that's not ideal. A parent with three kids is going to get allocated up to 4 times, and only the last one actually matters. Is there a way to wrap all the update code into a packet and then execute the Allocation code? I suppose I could do something like: Ruby code:
|
# ¿ Oct 22, 2014 16:35 |
|
Makes sense. I've just been read to keep as much out of the controller as possible I'm wary of doing anything there. Makes sense that it's something the controller should do over anything else. Where's the conventional place to put PizzaAllocator? In with the models?
|
# ¿ Oct 22, 2014 17:16 |
|
I do ajax this way:
|
# ¿ Nov 7, 2014 01:15 |
|
kayakyakr posted:e: ^^ that's a very old-school way of doing it. Do you know of a tutorial for a more modern way of doing it? I learned that from the rails docs.
|
# ¿ Nov 8, 2014 15:52 |
|
First thing I did at my new job was look in /test/. Nothing.
|
# ¿ Nov 21, 2014 20:41 |
|
Ruby code:
|
# ¿ Nov 23, 2014 19:22 |
|
At the place I'm working all the view partials are put into concern folders, (like app/views/model_name/concerns/_partials.html.erb). Is this common?
|
# ¿ Nov 25, 2014 15:47 |
|
I've got a best practices question about Ajax stuff: I have a model Article, and I want to be able to update particular attributes of that article through an AJAX form on the Article's Show page. These attributes could be Title, Status, and Footnote. Right now, I am placing the forms for updating these attributes in their own partials, and placing those onto the show page inside hidden div tags. The forms target the Update action of the ArticleController. I'm manually handling the Ajax calls by binding the ajax:whatever events in articles.js, and this feels a little smelly. Would I be better off having dedicated update_title action, with its own update_title.js.erb file to handle the Ajax call? I'm a little afraid of overloading the controller. Edit: Maybe in update.js.erb I could do something like JavaScript code:
MasterSlowPoke fucked around with this message at 16:14 on Jan 16, 2015 |
# ¿ Jan 15, 2015 21:24 |
|
Is there a fast .xls reader for Ruby? I don't want to write anything or look at fonts or whatever, I'm just interested in the data. I've tried out Roo, but it's taking 3-4 minutes to load a very small (33x10) spreadsheet.
|
# ¿ Jan 19, 2015 17:55 |
|
I've tried bothcode:
|
# ¿ Jan 19, 2015 19:30 |
|
The answer is don't set a class variable unless you have a really good reason to.
|
# ¿ Jan 23, 2015 01:45 |
|
!(session[:user_id].nil?) if session is nil, is_logged_in? => !(true) => false if session is set is_logged_in? => !(false) => true I'd probably have session[:user_id].present?.
|
# ¿ Jan 31, 2015 19:24 |
|
I've got the feeling I'm not writing my tests properly:Ruby code:
Ruby code:
|
# ¿ Feb 23, 2015 20:26 |
|
Gotcha, thanks. I've written these tests for my user model using that info, do they look good?Ruby code:
As an aside, I don't like this pattern on my admin scope: Ruby code:
Ruby code:
|
# ¿ Feb 24, 2015 18:06 |
|
It doesn't say the poop goes in the toilet. The flush is probably just so no one else knows what you're up to in the stall.
|
# ¿ Feb 27, 2015 22:58 |
|
Maybe go about it backwards? Something like Ruby code:
Two simple queries instead of a join query. On phone so may be typos.
|
# ¿ Mar 7, 2015 03:00 |
|
IIRC AR will still store UTC in the database, but auto convert it when pulling it out.
|
# ¿ Mar 17, 2015 17:21 |
|
I'd probably just do a really simple scope:Ruby code:
|
# ¿ Mar 18, 2015 22:13 |
|
I'm writing some tests for our Cancan implementation, and they're a little repetitive:Ruby code:
|
# ¿ Mar 20, 2015 19:53 |
|
If you want to learn user authentication, follow the relevant chapters (looks like 6-10) in the excellent Rails Tutorial. If you're not really interested in user auth, use devise. Any solution you throw together without learning the Why's is going to be hopelessly insecure. Finally, whatever you do, never store plaintext or encrypted passwords. MasterSlowPoke fucked around with this message at 00:04 on Mar 28, 2015 |
# ¿ Mar 27, 2015 23:56 |
|
Thought there was a CI thread but I can't find one. Does anyone know if theres a way to configure Jenkins to post a generated web page? My testing generates a coverage report using simplecov, and I'd like to be able to access it on the CI server as well. It's not hugely important, but it'd be nice if it's possible. edit: Found this plugin for posting Rcov files. MasterSlowPoke fucked around with this message at 16:51 on Mar 31, 2015 |
# ¿ Mar 31, 2015 16:24 |
|
EVGA Longoria posted:Have you tried CodeClimate? How does it compare? We use CC here and it's ok, but if there's better test coverage I could look into, that'd be great. I've not. I'm still rather new at Rails and testing in particular. Where I'm working we had no tests when I started - the codebase is mostly just 400 line controller methods. I went with Simplecov just because it was recommended in some tutorial. I don't like that SimpleCov only provides reports for files called in the tests - there are a lot of models and such that I haven't gotten to yet, and I'd just like to see them so I could eventually check them off. If I wasn't working with a legacy code base that wouldn't be such an issue.
|
# ¿ Mar 31, 2015 18:30 |
|
Buckhead posted:I need to process a string word by word and see if certain words match a condition, then compile the string back together. Basically, I am pulling social media posts and want #s and @s to show up in blue. Ruby code:
edit: okay I must have opened this tab a long time ago and never looked at it MasterSlowPoke fucked around with this message at 18:35 on Apr 6, 2015 |
# ¿ Apr 6, 2015 18:32 |
|
I'm just about to use Resque, is there anything I should know about it or should I jump to something else before I'm in too deep?
|
# ¿ May 14, 2015 22:04 |
|
What is the contents of the @config hash there?
|
# ¿ Aug 26, 2015 15:49 |
|
It's too much data, so if it's lost then it's easier to replace than a backup hard drive?
|
# ¿ Nov 11, 2015 19:30 |
|
Sewer Adventure posted:Because it was a has_one relation, assignment doesn't need a save call When I ever have to use the production console like that I make sure to use sandbox mode.
|
# ¿ Nov 14, 2015 16:23 |
|
Should be as simple as Board has_many :boards, belongs_to :board.
|
# ¿ Feb 19, 2016 17:03 |
|
https://www.youtube.com/watch?v=mdqLMkPqjOQ
|
# ¿ Apr 19, 2016 04:06 |
|
It looks like that's going through and updating every cart to follow the new policy of grouping LineItems together. It is triggered once, when you run your migration. It's odd that there's no structural changes that enforce or allow the policy in that migration - I would have added the quantity column there instead of an earlier migration if I was writing that tutorial. I think it might just be a demonstration of how to write a reversible complex migration, and showing that migrations can do more than just alter tables.
|
# ¿ May 30, 2016 18:21 |
|
Simplist way would be to docode:
|
# ¿ Jun 28, 2016 19:58 |
|
I bought the book Rails 4 Test Prescriptions and it was pretty good.
|
# ¿ Jun 29, 2016 20:31 |
|
I just set up Sentry this week. Haven't spent a lot of time with it but it looks pretty neat.
|
# ¿ Jul 1, 2016 19:12 |
|
It's kind of like how += works: a += 1 a = a + 1 Are both the same statement. So: a ||= 1 a = a || 1 Means that a, if it's a falsy value (like nil), will equal 1. &= and |= aren't common, but they'll be # for a = 9 a = a & 5 a &= 5 Both result in a being set to 1. Same thing for |=. Bitwise AND and OR simply aren't very popular or generally useful, so no one writes about them.
|
# ¿ Jul 11, 2016 16:41 |
|
You're probably just being tricked by the output. Consider:Ruby code:
&& is the Logical AND operator. It look at the truthyness of the operands, and returns the last value looked at. 7 and 5 are both truthy, so it returns the last operand. This is useful for short circuiting - if you do: if nil && 5 It first looks at nil, and as that's falsy, ends looking and returns nil. This causes the if check to fail.
|
# ¿ Jul 11, 2016 16:56 |
|
Right now we have two databases. We need to get data (about 300k rows) from a secondary database, use 3 different primary keys to the primary database to retrieve information, and then write the data to a file. The current system (all with active record) is this: Establish connection to the secondary database Make a request for the 300k rows Reconnect to primary database Map the rows using the primary keys, then inject a hash to hold the data from those rows Then create an array collating the data. It's slow as gently caress and I'm sure it's far from the standard way to go about it. What's the correct way to get massive amounts of data connected between databases?
|
# ¿ Oct 30, 2018 19:30 |
|
It's using ActiveRecord::Base.establish_connection to swap databases. The really slow part is pulling the associated data from the primary database. It's just a huge find query reduced to a hash: Addresses.where(id: query.map(&:address_id)).pluck(:fields).inject({}) {...} It's typically done in the job queue.
|
# ¿ Oct 31, 2018 00:40 |
|
After switching back to the database with the Addresses table I can't pluck anymore right? That does another select query and I no longer have access to that table.
|
# ¿ Oct 31, 2018 02:20 |
|
|
# ¿ Apr 27, 2024 11:40 |
|
I'd like to set up the model to use the database, but unfortunately due to the way they set up their databases (there's multiple versions of this database, 1 per year for some reason I'm not sure) I can't set that up in a non-hacky way. Did some research and decided on doing something kind of like pluck_in_batches: Ruby code:
|
# ¿ Oct 31, 2018 06:53 |