Chris Oliver

Joined

293,420 Experience
93 Lessons Completed
295 Questions Solved

Activity

Posted in Pull data from another table in a lookup

Oh I gotcha now! Yeah that makes sense. So then you are correct having both the venue_id and product_id on that table. The naming I believe is what threw you off because you actually need a bunch of records of the BackBar per venue.

class BackBar < ApplicationRecord
  belongs_to :product
  belongs_to :venue
end
class Product < ApplicationRecord
  has_many :back_bars
  has_many :venues, through: :back_bars
end
class Venue < ApplicationRecord
  has_many :back_bars
  has_many :products, through: :back_bars
end
# controller
@products = @venue.products

Posted in Pull data from another table in a lookup

Yeah, for a many-to-many relationship, you'll actually need a join table and has_and_belongs_to_many will use that to connect everything. Thats the only way for your backbar to have many products and your products to belong to many backbars.

Posted in Pull data from another table in a lookup

Yeah, sounds like your relationship is backwards in the database where a BackBar belongs_to :product, when it should actually be a Product belongs_to :back_bar.

Depending on what you want to do now, you can use something like this to work with their API: https://github.com/gimite/google-drive-ruby

You'll just pass in the access token you received from omniauth into this library to hit their API.

Posted in Setup Windows 10 Discussion

I used rbenv just fine when I initially set things up. My Windows 10 install got corrupted so I need to reinstall it and try this again sometime to see how things have changed.

Posted in Pull data from another table in a lookup

If I'm following correctly, I think your query @venue.backbar.products is what is causing that. If you have a has_many :products on BackBar, then you also need the back_bar_id on the Product table to associate them.

Posted in Group Chat with ActionCable: Part 2 Discussion

You would want something like this:

add_index(:chatroom_users, [:chatroom_id, :user_id], unique: true)

Can you share your whole file?

What are you having trouble with? The if statements around your code should be mostly the same.

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.

Posted in Direct File Uploads to S3: Part 3 Discussion

Might want to undo that? I'm not sure.

Posted in Direct File Uploads to S3: Part 3 Discussion

Absolutely, glad you enjoyed it! :)

Posted in Direct File Uploads to S3: Part 3 Discussion

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 %>

Posted in How do I create hierarchical data with a model?

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!

Posted in How do I create hierarchical data with a model?

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