Activity
Posted in store_accessor with jsonb nesting
Honestly, I think you're better off using this than trying to build your own implementation. It'll let you set defaults and types for each attribute which will make it significantly easier to work with: https://github.com/devmynd/jsonb_accessor
The types are going to be a problem when you start using forms because they won't automatically be cast to booleans, etc. That makes for a mess.
You shouldn't need the HashSerializer
because that's what store_accessor
implements for you. It doesn't do with indifferent access, but it will convert back and forth from jsonb to a hash.
To set defaults, just setup a default value after initialize:
class Foo < ApplicationRecord
store_accessor :permits, :bar_permit, :baz_permit
after_initialize :set_defaults
def set_defaults
self.permits ||= {
bar_permit: {
key1: [],
key2: "Bar",
key3: false,
key4: false,
key5: false
},
foobar_permit: {
key1: [],
key2: "Bar",
key3: false,
key4: false,
key5: false
}
}
end
end
This tool can translate to and from Coffeescript: http://js2.coffee/ 👍
Posted in Exporting Records To CSV Discussion
It's not an Array, it's an ActiveRecord::Relation object. They act like Arrays, but are very different.
Posted in Is there a better way to simplify this?
You can customize it, but that's the inferred default name.
Might want to undo that? I'm not sure.
Absolutely, glad you enjoyed it! :)
If you're getting the fileupload is not a function, then maybe jquery fileupload is not included in your application.js file?
Posted in Is there a better way to simplify this?
For the most part, yes. You also need to add a products_count
integer column to categories, default it to 0, and then update the records in the database to have the correct amount. Then your counter_cache will update that column and your query of Category.where("products_count > 0")
will work.
Your view just references the products count, so you can actually print out the new column value without loading any products making it even more efficient.
<% @categories.each do |category| %>
<div class="pad1 list-row">
<div class="space-left4 pad1x row-details">
<div class="details-name"><%= category.name %></div>
<div class="details-sub">Products: <%= category.products_count %></div>
</div>
<div class="row-actions"></div>
</div>
<% end %>
Oh wonderful, I haven't heard of that one and by the docs and activity it certainly looks like it's well maintained. I'll have to try it out!
There are a handful of different ways you can do it. This is actually a nice little blog post talking about 3 options and their pros / cons. Generally you're going to want ones with fast reads because that's going to be the more common query.
https://www.leighhalliday.com/tree-structures-in-your-rails-models
Posted in Is there a better way to simplify this?
Yup, once you've gathered your categories that have products efficiently, you can preload them with includes
as well.
Great idea. The google API is definitely not the simplest API to work with in Rails because it's so generic. I will definitely try to cover it soon.
For some help in the meantime, the main things you're going to need is to make sure that your scope is set properly for Drive authorization. That will make sure once you have keys you can access Drive with them. I would probably recommend using this library over the google one because this does the same thing but is setup nicely to work within Omniauth which Devise supports. Configuration is super easy and will save you a lot of time: https://github.com/zquestz/omniauth-google-oauth2
Thanks Matt! That means a lot. :D
Yep, exactly! You can also add an index on that table that includes those two columns in order to speed up the query this validation executes to check validity. That'd be the only other thing you'd probably want to add.
Posted in In-App Navbar Notifications Discussion
Just render them in your navigation snippet so that they're already there when the page loads. It adds query time which slows down your page load, but won't make them appear later. The other option is to just style your CSS with that space already taken up so the view doesn't shift and cause your eye to look at it.
Posted in Is there a better way to simplify this?
One thing you could do is to set up a counter cache on Category so that you can store the number of products on it. Then when you query you can query for Category.where("products_count > 0")
to improve your query and remove that if statement in the view.
Posted in Liking Posts Discussion
Main reason is it's such a simple feature, no need to use a gem. Always great to have one less dependency, less likely to break on upgrading Rails versions, etc. And the benefit is I can customize it and use it exactly how I want.
Thanks man! Fixed that and glad you're enjoying the Shrine episodes. :D
You're making super good progress. And I know right? I started with GW-Basic, VB6, some C++, Java, back in my early days and it wasn't till I found Python that I felt like I could actually be productive and build full applications myself. Rails sped that up even quicker!