[Solved] Undefined method ‘[]’ for nil:NilClass when trying to write to db in active record [closed]

I figured out the reason for ArgumentError: uncaught throw “Error in record 314675: undefined method ‘[]’ for nil:NilClass’ was primarily from an incomplete set of input variables that is required by the def init method. Because this was an ArgumentError that was thrown from the function1 method, I was only invested in debugging function1. The … Read more

[Solved] How do I prevent the addition of the same product to the cart [closed]

Add uniqueness validation on Cart.product_id, scoping by Cart.id: class Cart < ApplicationRecord validates :product_id, uniqueness: {scope: :id} end But beware of race conditions. UPDATE: If no actual Cart model add validation to LineItem: class LineItem < ApplicationRecord validates :product_id, uniqueness: {scope: :order_id} end UPDATE 2: refactor add method with find_or_initialize_by: def add @cart.save if @cart.new_record? … Read more

[Solved] Email Regex. Is this regex expression appropriate? [duplicate]

What do you think of the above expression for email validation. For one, it doesn’t accept my address. So, there’s obviously a bug in there somewhere. I suggest you read RfC5322 carefully, it describes the valid syntax for addresses quite clearly, although unfortunately it hasn’t yet been updated for IDN. solved Email Regex. Is this … Read more

[Solved] How do I stop circular “followers” in a model?

You could write a custom validator function: For example(assumption, you have a followed_users method which returns all the users that the current user is following, and a follow method, taking a user id and “following this user.): class User < ActiveRecord::Base has_many :users, inverse_of :user, as: followed_users validates :verify_no_circular_followers def followed followed_users end def follow(user_id) … Read more

[Solved] Convert or Execute an CURL Request for Rails Application

Use the rest-client gem.You can make HTTP request from your controller using this. Their github page has examples explaining how to make reuqest along with paramters. A simple get request may look like this: RestClient.get ‘”https://graph.facebook.com’, {params: {‘scrape’=> true, ‘access_token’ => ‘your_access_token’}} solved Convert or Execute an CURL Request for Rails Application

[Solved] Twitter bootstrap datepicker is not working in rails

Add //= require_self to application.js this includes its own file contents too. This has to be done explicitly. also keep the application.js formatted //= require jquery //= require jquery_ujs //= require bootstrap //= require bootstrap-datepicker //= require picker //= require_tree . //= require_self $(function() { $(‘#dp5’).datepicker() }); // Do not keep bank spaces between included … Read more

[Solved] Convert string to XXX

What you need is hash and not string. You can use it like this, hash = {id: 2} User.where(hash) And if you really want to use string you can do it like this, string = “id = 2” User.where(string) If you want to execute the string you can use eval. In that case it will … Read more

[Solved] What would the ruby equivalent be to this python script?

Python: print (“{0:5s} {1:7s} {2:9s} {3:6s} {4:25s} {5:s}”.format(‘Rank’, ‘Points’, ‘Comments’, ‘Hours’, ‘Sub’, ‘Link’)) Ruby: puts “%-5s %-7s %-9s %-6s %-25s %-5s” % [‘Rank’, ‘Points’, ‘Comments’, ‘Hours’, ‘Sub’, ‘Link’] Alternatively: puts sprintf(“%-5s %-7s %-9s %-6s %-25s %-5s”, *[‘Rank’, ‘Points’, ‘Comments’, ‘Hours’, ‘Sub’, ‘Link’]) 1 solved What would the ruby equivalent be to this python script?