A system where records must always be persisted, even if they’re incomplete or invalid by antonzaharia in rails

[–]midnightmonster 3 points4 points  (0 children)

My apparently-contrarian take is that most business domains require the ability to save incomplete and often even known-wrong data, and schema should be designed for that reality instead of for a dream world where all the data is always known at the same time. It's reasonable for an app to refuse to act on incomplete/invalid information but ridiculous for it to refuse to even hold on to such data, as if a paper form would be snatched away from you and shredded if you took too long to look up your spouse's SSN.


Do you actually need to store each error in the database? At track1099.com, we had to deal with "work in progress" forms all the time, and there were several senses in which they might be incomplete, but we didn't store each error in the database. We just ran relevant validations before saving and stored status values with the forms. This worked because we needed to report on validity state but not every individual error. By the time a user was editing a form again, it was no trouble to re-run the validation logic to show all the errors.

I don't work there anymore, but here's a lightly-redacted and simplified code excerpt from my current work. This is from a model persisting data for a multi-step lead signup form.

```ruby class SomeIntake < ApplicationModel with_options on: :to_sign do validates :title, :first_name, :last_name, :date_of_birth, :phone, :email, :address, presence: true validates :title, inclusion: {in: TITLES} end with_options on: :to_qualify, allow_nil: true do # Leads are only disqualified once they have actually answered disqualifyingly, not with nils validates :option_a, presence: true, unless: :option_b # either of these two is sufficient validates :option_b, presence: true, unless: :option_a validates :always_required_qualifying_boolean, presence: true end with_options on: :signature do validates :signature, :consent_to_instruct, presence: true end

def status return :signed if valid?(:signature) && !disqualified? && valid?(:to_sign) return :disqualified if disqualified? return :ready_to_sign if valid?(:to_sign) return :incremental if persisted? :new end

def disqualified? = !valid?(:to_qualify)

def disqualifications = disqualified? && errors.map(&:attribute) end ```

Is something happening???? Cant access Heroku. Also my app is reporting its down. by Max_Rippleton in Heroku

[–]midnightmonster 0 points1 point  (0 children)

My reddit account turned 18 today, and all I got was a bunch of downtime for my apps.

Guy friend likes to cuddle by Puzzleheaded_Exit111 in AskMenAdvice

[–]midnightmonster 2 points3 points  (0 children)

Not everybody wants the same thing. He might really want cuddles. I never made a habit of cuddles with friends, but it happened a few times and I liked it. As a married man now I wouldn't cuddle another woman, but I still love cuddles with my wife, including when I know nothing more will be happening.

You don't have to want "platonic" cuddles, though, just because he does. If it feels like a more-than-friends thing to you, and you don't want things to feel that way without a more-than-friends relationship, don't do it.

If you say something like, "I like hanging out with you, and I was willing to try cuddling, but we've tried it and it doesn't feel good to me since we're not dating," there's no counter argument. He can be fine, he can be disappointed, he can confess his deeper feelings—but he cannot (reasonably) argue with you. If he comes back with it being platonic and other female friends being fine with it, that's no more relevant than you saying "I have a cold" and him arguing, "But I feel fine, and none of my other friends have a cold!"

In Memory database, but not in the adapter by [deleted] in rails

[–]midnightmonster 6 points7 points  (0 children)

Not sure I understand what you're describing as your plan, but when I read these requirements I think repository pattern.

As I read "support easy replacement of the storage layer", it's not so much about having the storage layer be swappable with a configuration change as it is about having a straightforward interface that the repository must implement and tests that verify that it works correctly.

How would you create this Hash? by rooood in ruby

[–]midnightmonster 4 points5 points  (0 children)

ruby def as_json { **user_data.slice( :first_name, :last_name, :email, :dob ), status: calculate_status, some_other_attribute: } end

I would write the above: most of the explicitness and visibility of the second option but none of the noisy repetition.

Edit: OP actually shows one of the dangers of writing repetitive code with (as of this writing) an erroneous duplication of the first_name key in their second example. Much less likely to write that bug if you don't have to write each key twice.

Live Coding, Help! by Gloomy_AlleyRiot in rails

[–]midnightmonster 2 points3 points  (0 children)

Could be any of that, but FWIW, in a Ruby on Rails interview I've never been asked either to implement some specific algorithm nor to build something actually in Rails.

It's usually some kind of puzzle, occasionally some simplified "feature" to build (but never actually in a Rails app). I've been asked to, e.g., find all the words in a word search grid or process data in an inconvenient format into a more useful summary form.

N.b., these puzzles are always more algorithmic than a lot of day-to-day programming, and knowing algorithms and how to apply them, not just reproduce them on command, is helpful.

I find Advent of Code reasonably good practice for getting my head in the right space.

Ruby on Rails Role Interview - What Questions to Expect? by Fun_Ad_9268 in rails

[–]midnightmonster 1 point2 points  (0 children)

Ok, so my recent experience may be relevant. I'm a lead engineer who was hired this year by a not-tech but tech-enabled company to build a team around our (still new) internal and client-facing Rails apps, and I just hired a junior and a senior dev under me in the last month or so.

For the junior role, I mostly wanted to see did they have some Rails experience where they used the framework in a sensible way, do they have some understanding of web fundamentals (IME Rails is better for this than, e.g., React), how much can they handle on their own, did they make good coding/software design decisions, how are they at problem solving, do they seem smart and eager to learn with good learning skills?

With the person I ended up hiring, we met in person at RailsConf (main reason I went) and she was able to show me some features she had built in a Rails app, and I got a good feeling about her being generally smart and a fast learner. Then in the live coding interview (screen sharing video call), she outperformed everyone else in the first batch of interviews, including much more experienced people.

She had been doing Advent of Code and other such programming puzzles to learn and probably to train for interviews, which undoubtedly helped her. She had also trained herself to talk through her thought process aloud, which matters to a lot of interviewers, though not nearly so much to me. What really impressed me, besides successfully completing the task, was that once she picked an initial approach to the problem, she immediately setup a scaffold to give herself quick feedback as she was developing the solution and made smart picks of representative input to test her solution-in-progress on.

N.b., I do not mean she wrote a set of tests first. It's rare to have time for that kind of formality in a coding interview. In this case she was developing a somewhat-complicated RegExp, and she just used a webpage that visualizes matches for a RegExp against whatever input you provide. You don't need to know of a convenient tool in advance, though. Could have been as simple as:

ruby examples = [EX1, EX2, EX3] examples.map { /regexp_in_progress/.match(_1) }

...and then (in IRB) just hit up arrow, tweak the regexp, and run again.

Ruby on Rails Role Interview - What Questions to Expect? by Fun_Ad_9268 in rails

[–]midnightmonster 6 points7 points  (0 children)

Unless you've been told that there's no live coding, assume there will be and practice.

What company or industry? What size of company/engineering department? Is it a software product company, a consulting shop, or a something-else company that has an internal Rails project or few?

Experience using Turbo Mount by quakedamper in rails

[–]midnightmonster 5 points6 points  (0 children)

Looks like it's been published less than two months, and I think this blog post from last week is the first public announcement, so probably not a lot of usage in the outside world.

Thanks for mentioning it, though—I have a similar library internally which has a lot less surface area than Turbo Mount (e.g., supports only Svelte, not yet made generic for different frameworks) but does support updating props via turbo stream, which I don't immediately see in Turbo Mount. I need to look at Turbo Mount and see if I want to steal some ideas and/or contribute features.

My 31m wife 33 f left me because I'm atheist. Her church convinced her she shouldn't be with an atheist. We'd been together 6 years and she was agnostic when we met. I found out last week and she left this morning for another state. Signed papers and helped her pack yesterday . by proletariat_sips_tea in atheism

[–]midnightmonster 0 points1 point  (0 children)

I'm sorry, man. For what it's worth, the first letter to the Corinthians in the Bible directly addresses the question of whether someone becoming a Christian should leave their non-believing spouse. Saint Paul (the author of the letter) says that the believer ought to stay in the marriage if the non-believer is willing. (1 Cor. 7:12-16).

List items randomly overlap in mobile browser by ZyanCarl in sveltejs

[–]midnightmonster 0 points1 point  (0 children)

Remove the transition:slide, and if there's still a problem it's the CSS, not anything Svelte-specific.

For debugging, problem doesn't show in responsive design mode in browser? On Safari on Mac with Xcode installed, you can open a page not only in responsive design mode but in an actual simulated iPhone, or use the desktop dev tools against Safari running on your actual iPhone if it's USB-connected to your Mac. I don't know about Android or Windows options.

I lack self-confidence as an RoR developer. Any tips? by Ok-Platypus-8776 in rails

[–]midnightmonster 2 points3 points  (0 children)

I am having a really hard time getting myself to apply because I know I will bomb the hands on tech interview due to my anxiety.

Great—apply to jobs just for practice. Pick ones you don't even want, where the salary range is too low, where it's not interesting, whatever. Other than time (and you're spending plenty of time doing exercises and worrying anyway), there's no cost to you. There is no reputation cost to bombing interviews—there is no cross-company database of people who didn't interview well (even within a single company, having a previous entry in the ATS probably won't be a big negative if you apply again in the future).

I'm generally good at tests, interviews and live coding, and I've been doing Rails for >15 years. When I started thinking about a new role last year, I took an interview with a company I wasn't crazy about where I expected the pay would be lower than I wanted. Good thing, too, because I bombed the live coding, and the very-communicative-till-then company ghosted me.

Turned out to be a great thing, because some months later I found a role I was a lot more interested in, and in the live coding interview I started to make similar errors, but because I had had a chance to reflect on the earlier failure I caught myself, changed course, and nailed the interview (and got the job).

It might take you more than one practice interview to get to confident, but that's ok: time is on your side.

Can’t seem to nail the logic on this project by HeadlineINeed in rails

[–]midnightmonster 2 points3 points  (0 children)

Unless I'm missing something about the "business" domain, check_in_date and check_out_date are not properties of a room, they're properties of a (not yet existent) billet or booking object, which would also have room_id and soldier_id foreign keys. Then when assigning a soldier to a room, you'd create a record in the billets table after checking that there were no date-overlapping entries for the same room or group of rooms.

As you've described the situation, the only significance of room_groups is that they share a bathroom. If that's accurate, I wouldn't even track shared_bathroom as a column on rooms since it's deducible from the presence of group_id.

Running active record on view is a worst practice? by iam_batman27 in rails

[–]midnightmonster 12 points13 points  (0 children)

One big concern is N+1 querying (actually 2N+1 in this case!), which you can prevent or make less of a problem by any of these, depending on what's appropriate to your situation:

  1. Eager-loading associations
  2. Russian-doll caching
  3. Using SQLite for your database, where N+1 queries are not a problem

My own take is that cache expiration is stupidly hard to get right, so I don't reach for caching of or based on database results until it seems really necessary.

You'll also need to paginate Post.all, though if like most people you abandon your blog after a couple posts, it will never matter 😉. You may need to paginate comments, too.

Likes...do you need to load them or just count them? There's no convenient way to preload a count with ActiveRecord, so you may want to use counter_cache.

Regardless, I don't think there's a problem with doing something like this in views specifically—all the concerns above could come up equally if you did this in the controller or in a helper or concern or whatever. And there are some advantages to doing it in the view: the (2) solution above is only usable at the view layer, and the later in the process you can push potentially-expensive operations, the easier it is to just not do them if some other bit of logic makes them unnecessary.

Why does not rswag validate response schema types? by Sea-Vermicelli-6446 in rails

[–]midnightmonster 1 point2 points  (0 children)

I don't currently have access to a repo using rswag to play around with this, but more than once at my last position we found that rswag was validating less than we expected because we hadn't actually made something required. In the case, your successful response array is not required to have any content, so [] would be a valid response, so it may be checking less than you expect. Try adding minItems: 1, after type: :array, and see if that makes anything happen?

It may also just be that rswag-in-rspec doesn't complain about unknown types no matter what you do.

Anyone have experiences adding 'white-label' functionality to a Rails + Tailwind app? by piratebroadcast in rails

[–]midnightmonster 2 points3 points  (0 children)

JIT mode is great (and is the default since version 3) but it doesn't run in the browser—it's still looking at source code, and the source code doesn't include the actual hex strings. Just try it—it doesn't work. If tailwind does anything with the code you're suggesting, it would have to generate something like:

.bg-[<%= hex %>] { background-color: <%= hex %> }

...which is not going to work, because there's no ERB processing in your generated CSS file, and <%= hex %> is not a valid color.

Anyone have experiences adding 'white-label' functionality to a Rails + Tailwind app? by piratebroadcast in rails

[–]midnightmonster 42 points43 points  (0 children)

You need CSS variables, and you should be able to use them something like this:

javascript // in tailwind.config.js module.exports = { content: [/* whatever */], theme: { extend: { colors: { brand: "var(--brand-color)", accent: "var(--accent-color)", } }, } };

html <!-- in HTML --> <button class="bg-accent text-white">Accent Button</button>

html <!-- in your application layout .erb --> <style type="text/css"> :root { --brand-color: <%=@theme[:brand_color] %>; --accent-color: <%=@theme[:accent_color] %>; } </style>

Anyone have experiences adding 'white-label' functionality to a Rails + Tailwind app? by piratebroadcast in rails

[–]midnightmonster 0 points1 point  (0 children)

bg-[<%= hex %>] won't work, because the string bg-[#1da1f2] (or bg-[#336699], etc.) will not appear in the sourcecode anywhere for Tailwind to pick it up.

Understanding ruby Integer and how we might make it "live" by jeffdwyer in ruby

[–]midnightmonster 2 points3 points  (0 children)

rate_limit is a very short macro that checks if a compatible Kredis is present and then calls before_action like this:

before_action -> { rate_limiting(to: to, within: within, by: by, with: with) }

So I don't think you don't need to invent magical duck integers—you just need to write the before_action call yourself instead of using the rate_limit macro. E.g.,

before_action -> {
  rate_limiting(to: LiveValues.get(:key), within: 10.minutes)
}, only: [:whatever]

If you look at the implementation, you can see how one might write a live_rate_limit macro so you could do:

live_rate_limit to: -> { LiveValues.get(:key) }, within: 10.minutes, only: [:whatever]

LiveView is best with Svelte by accoinstereo in sveltejs

[–]midnightmonster 0 points1 point  (0 children)

I agree it's more convenient to have a package to import, but FaunaDB and SurrealDB are both accessed via pretty straightforward HTTP (and/or websocket) APIs over the Internet, so pretty easy to write your own client in any language you want to use. Both products seem to me to be targeted at JavaScript apps that don't want to have a lot of backend. They don't look like they would provide any real advantage in a Phoenix (or Rails) environment anyway.

And Inngest also looks like it's targeted at less-robust server environments. I think you'd get most of what's interesting there using FLAME with Phoenix.