Activity
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
Posted in Why my rails production server reaches to 100% CPU usage and making app to be unresponsive?
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!
Posted in Why my rails production server reaches to 100% CPU usage and making app to be unresponsive?
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.
Posted in Why my rails production server reaches to 100% CPU usage and making app to be unresponsive?
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?
Posted in Why my rails production server reaches to 100% CPU usage and making app to be unresponsive?
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?
Posted in Why my rails production server reaches to 100% CPU usage and making app to be unresponsive?
Chances are this isn't something anyone here is really going to be able to help with without a lot more information. Issues like this you need to scour all of your logs in /var/log
and your rails logs + passenger logs. Are you sure it's even your rails app that's causing it and not something else on your system? What process is really causing your CPU to max out and how are you determining that?
Check these links to get you going in the right direction for diagnosing your problem:
http://unix.stackexchange.com/questions/130955/how-to-troubleshoot-high-load-when-there-are-no-obvious-processes
https://www.tummy.com/articles/isolating-heavy-load/
http://www.linuxjournal.com/magazine/hack-and-linux-troubleshooting-part-i-high-load
https://bobcares.com/blog/how-to-troubleshoot-high-load-in-linux-web-hosting-servers/
Posted in What's the best way to install a robust system which checks if an event is over-booked in Rails?
Based on what I see, you're saving the booking, reloading the event, and then checking if it is now over filled. You need to check first if the requested number of spaces is greater than the number of available spaces before you ever call @booking.save!
. So you'll want a separate function that gets called in your create function before you save which checks if that event has enough slots available. Also, I'm not sure how you have everything setup, but I would limit the number of bookings a user can select based on the number of slots left at that moment in time as a UX improvement so you don't even let the client think they're going to be able to book that many slots, just to then have the system tell them "sorry, you can't do that!"
You should have a column in your event table that lets you set the total # of spaces available when you're creating the event itself. So to see how many slots are available for that event, you'd just @event.spaces_available
. When you're showing all the events, just do something like:
<% @events.each do |event| %>
<%= event.name %>
<%= event.date %>
<% if event.spaces_available == 0 %>
Sorry, no spaces left!
<% else %>
<%= event.spaces_available %> spaces left - book now!
<% end %>
<% end %>
Anytime you're working with booking events / appointments you need to be aware of race conditions. You need a way to temporarily "reserve" slots while your user is completing the checkout process. There are a ton of ways of handling this, you'll just have to figure out what works best for your setup.
before_action
is definitely the way to go! Just treat it like any other method... I use this for a contact form on every page...
#controllers/application_controller.rb
before_action :set_contact
private
def set_contact
@contact = Contact.new
end
then your partial is setup as you'd expect:
#views/shared/_contact_form.html.erb
<%= form_for @contact, url: user_contact_path do |form| %>
Hah I hear ya - I've been doing web "stuff" for well over 15 years but always stuck to CMS's like Joomla or WP and never really learned how to program, only just how to hack around what someone else did to make it fit my needs... only in the past year did I pickup Rails and boy do I wish I would have started much sooner!
It's all good though, we're here now and as long as Chris keeps pumping out these awesome videos everything will be right as rain! :)
Posted in Devise: Add a select to my signup form
I'm not 100% sure what you're trying to accomplish, but you're looking at either needing to just rename the routes or you may need to override the update method.
If it's just a route renaming issue, it should be something like this:
devise_for :users, :controllers => { registrations: 'registrations', sessions: 'sessions' }, :path => '', :path_names => {:sign_in => 'login', :sign_up => 'register'}
Of course update to whatever your names are...
Lines 41 - 62 is the update method on the registrations_controller and is what you're going to have to override to redirect to a certain action given whatever conditions you have. I haven't personally done it for this scenario so this is about as far as I can help for this one.
Definitely watch Chris' video on it, I believe that's where I finally figured it all out.
The keys things that stumped me were ensuring
accepts_nested_attributes_for :company
is properly set in the User model, and then ensure your sanitizers are set correct in registrations_controller.rb or application_controller.rb (depending on your setup):
devise_parameter_sanitizer.permit(:sign_up) do |user|
user.permit(:email, :password, :password_confirmation, :is_brand)
end
Then in your sign-up form:
<%= form_for resource, as: resource_name, url: registration_path(resource_name),
html: { class: "form-horizontal", novalidate: true } do |f| %>
and just call the :is_brand as if it's a normal field..
<%= f.select(:is_brand, options_for_select(Company.is_brands.keys.to_a)) %>
You may need to change your select method - this one is based on an enum being defined in the model to define companies
I think that's all there was to it... good luck! :)
Posted in Devise: Add a select to my signup form
Sure, I'll take a look. I learn as I go so if I've encountered it before then I can probably help!
You may have an issue with Rails 5, I'm still on 4.2.3 for the project I pulled that from. I did encounter your error a lot if I didn't have the proper before_filters set... so for mine its:
before_filter :configure_permitted_parameters, :only => [:create, :update]