Save 36% for Black Friday! Learn more

Jacob Montgomery

Joined

34,500 Experience
33 Lessons Completed
41 Questions Solved

Activity

Posted in Pull data from another table in a lookup

Sweet, I'll have to play around with it, I love a good editor! :)

Did you ever mess with Summernote? http://summernote.org/examples/#hint-for-mention
They support mentions out the box it looks like. I use it on a site and have had no real issues so far.

Anywho - thanks again, and great job!!

Posted in Pull data from another table in a lookup

Woah, very clean!

Think you'll be sharing some of the techniques you used? I'm digging this WYSIWYG editor!

Thanks Chris! :)

Posted in Pull data from another table in a lookup

Hmm, markdown isn't parsing for me even though it's showing up in the preview just fine... oh well, this is what I was trying to post as my example:

https://gist.github.com/nanosplit/b50241d66947fbcae3a1020d1f94ab8a

Sorry to spam! :)

Posted in Pull data from another table in a lookup

Lol naming can be a major PITA... just last night I spent a few hours just renaming things to be a little more future proof.

If you're able to, take some time to build prototypes and do proof of concepts. This will help clear up some of the naming issues that happen when first starting a project. Often you don't know what really makes sense until much further along in the project but by that time it's too late or a major undertaking to rename.

An episode that digs into the associations would be great! Just last week I finally figured out some of the magic in associations, the moment I realized you can designate your own primary / foreign keys and even name it however you'd like was one of the happiest moments in my life!

class Agent < ActiveRecord::Base
  has_many :ar_listed, class_name: 'Abor::AborRetsResi', primary_key: 'agent_id', foreign_key: 'list_agent_mlsid'
end

#this lets me do:
@agent_listed_properties = current_agent.ar_listed

#which returns all the listed properties that agent has! Before, I would have had to do something like:
@agent_listed_properties = Abor::AborRetsResi.where(list_agent_mlsid: current_agent.agent_id)

Of course this may not be the best way to approach this, but seeing how this all works was really eye opening!

Posted in Pull data from another table in a lookup

Hah, the devil is in the details... naming can be incredibly important. Your code is telling a story to other developers, so using descriptive names can go a really long way.

I was just reading more into the HABTM associations and they show to use the names of the two associated tables to make the name of the join table... so in your case this would be :venues_products. It's a little weird to say out loud but at least the name instantly tells you that it has something to do with venues and products.

This was an interesting problem, thanks for the challenge Alan, and thanks Chris for the nice solution! :)

Posted in Pull data from another table in a lookup

You may want to check out the has_and_belongs_to_many association

http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many

class BackOffice < ActiveRecord::Base
  has_and_belongs_to_many :products
end

class Product < ActiveRecord::Base
  has_and_belongs_to_many :back_offices
end

Again, Chris may be able to clear this up more... I haven't had much personal experience with HABTM so there could be some catches I'm unaware of.

Posted in Pull data from another table in a lookup

It kind of depends on products in this case. Will a BackBar ever have more than 1 product? And will a product ever belong to multiple back_bars?

If your BackBar will only ever have 1 product associated with it then you can store the product_id in the BackBar table. However, if a single BackBar can have multiple products, then you will have to let the products table store the back_bar_id so that Rails will know what products belong to what back_bars. The whole "Rails Magic" thing really is just letting Rails know which foreign_keys it has to look for in order to retrieve the records.

Just a thought, but would a :through association work better here? Something like:

http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

class Venue < ActiveRecord::Base
  has_many :back_offices
  has_many :products, through: :back_offices
end

class BackOffice < ActiveRecord::Base
  belongs_to :venue
  belongs_to :product
end

class Product < ActiveRecord::Base
  has_many :back_offices
  has_many :venues, through: :back_offices
end

I have no idea if this would work correctly for you but may be worth tinkering with unless Chris can say definitively one way or another?

Posted in Pull data from another table in a lookup

It makes perfect sense if you're saying that a BackBar has_many :products - the back_bar belonging to a venue has nothing to do with the association between the back_bar and the products table.

#get the first BackBar object
@backbar = Backbar.first

#get the products for @backbar
@backbar_products = Product.where(:back_bar_id => @backbar.id)

#this is the same as saying:
@backbar.products
# if you have the has_many :products association as stated above

Posted in Pull data from another table in a lookup

Look at error message, it tells you the problem:

PG::UndefinedColumn: ERROR: column products.back_bar_id does not exist

When you have a has_many association, the associated table has to have a column that will hold the parents ID. So in this case, you need to add a back_bar_id column to your products table. When you call @venue.backbar.products, it will look at the Product table and try to find any products that has an back_bar_id that matches the BackBar object you're currently working with.

Posted in Pull data from another table in a lookup

Looking at the association more... if what you're trying to say is that a Venue should only have one BackBar, and a BackBar has many Products, then your models would look like this:

class BackBar < ApplicationRecord
  has_many :products
  belongs_to :venue
end

class Product < ApplicationRecord
  #nothing needed here...
end

class Venue < ApplicationRecord
  has_one :back_bar
end

I believe this would now allow:

@products = @venue.backbar.products

I haven't actually tested this so it may completely blow up the universe as we know it... but I think that's possible :)

Posted in Pull data from another table in a lookup

If I'm not mistaken, your association is wrong. class BackBar would need has_many :products in order to use .includes() like this.

Check out http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations and http://apidock.com/rails/ActiveRecord/QueryMethods/includes for some extra details on includes.

I think you need to fix your association to something more like this:

class BackBar < ApplicationRecord
  has_many :products
  has_many :venues
end

class Product < ApplicationRecord
end

This would allow you to do something like:

@products = Backbar.where(:venue_id => @venue.id).includes(:product)

or if you just need the products that go with that backbar, then

@products =  Backbar.where(:venue_id => @venue.id).products

You could possibly be running into insufficient RAM for your deploy. I recently had to upgrade one of my droplets because during the asset pre-compilation it would max out the ram, jet the CPU to 100% then Ubuntu would shut it all down. However this was when I was attempting to use only a 512mb droplet and if I'm not mistaken you're running 4gb of ram so might be a slim chance that this is the cause unless you have some really big assets...?

Here's a couple of more stuff I found digging around the interwebz...

http://stackoverflow.com/a/24993595 <= this maybe useful to determine which task is overloading during deploy
https://github.com/capistrano/rails/issues/55

Unfortunately I'm not going to be much more help here since I use git hooks and not capistrano. Which, if you have the ability to easily spin up a new VM you could try to deploy using a different technique and see if it really is isolated to capistrano... process of elimination!

Posted in Pull data from another table in a lookup

So that's how it would be done - thank you for the example Chris!

Here lately I've been refactoring things down like James had suggested in an attempt to better understand how each entity in the MVC framework really works together. Even if it's overkill or unnecessary, I think it's been an incredible learning experience! The hard part is once you know how to abstract/refactor things out, when to do it and when not to do it...

Posted in Pull data from another table in a lookup

Alan, check:

http://tomdallimore.com/blog/includes-vs-joins-in-rails-when-and-where

https://gorails.com/blog/activerecord-merge

http://guides.rubyonrails.org/active_record_querying.html#scopes

I believe this should get you down the right rabbit hole. I'd have to dink around with your models to get something more concrete, I can't recall if I've ever done an includes in a scope. If you come up with a working scope please share if you can!

Ack, I'm sorry Srinivasa Varma, James Jelinek is right - 1.0 = 100%, so you want to be below 1.

If it's not always maxed out then you're probably going to have to sit and monitor top for awhile and pay attention to the cpu column for when the process starts to spike then snag the screen shot. To sort by the CPU field inside top, press the 'P' key (capitalized).

Also note that the CPU column can be a little funky if you're on a multi-core/processor system so check out http://unix.stackexchange.com/a/145249

Posted in Pull data from another table in a lookup

Haha, yes this really is a great little community! :)

Posted in Pull data from another table in a lookup

Whoops, looks like I had a few typos in there, sorry about that! I was however thinking the includes needed to be the plural form of the association but looking back at my own project it's indeed singular!

Do what Chris said and you'll be set! :)

Posted in Pull data from another table in a lookup

This may not be the best way, but usually when I do this kind of stuff I use the controller to build an array that has all my data in it that I need for the view. Is there an association between articles and brands? If so you could use includes(:brands)

@articles = Article.where(Brand_id: brand_ids).includes(:brands).map { |article| [article.details, article.brands] }

I'm assuming with the plural 'brands' that an article could have many brands. So in the above example, @articles.first[1] would list all the associated brands with the first article.

I don't see anything wrong there, all well below max.

Is there any other indication that you're using too much CPU? Maybe a specific page that takes a long time to load?

If you're referring to:

[ 2016-10-24 23:02:18.7328 21683/7fec0c265780 age/Cor/CoreMain.cpp:819 ]: Checking whether to disconnect long-running connections for process 21780, application /home/deploy/testprep_rails/current/public (production)

Then according to https://github.com/phusion/passenger/issues/1467#issuecomment-89545674 it doesn't look like that's the cause of your problem and just normal behavior. Is that all that you're basing your high usage off of or have you monitored something like TOP and noticed long periods of 100% usage?

https://linux.die.net/man/1/top

http://www.tecmint.com/12-top-command-examples-in-linux

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.