Questions tagged [ruby]

184201 questions
1

votes
1

answer
298

Views

How to add a custom column which is not present in table in active admin in rails?

in my rails application, i have installed active admin. in users index page, by default, all columns are getting displayed(User table columns). I want to add a custom column called 'become user' in this users index view (which is not a column in User's table). under this column i want to display use...
John
1

votes
1

answer
265

Views

Rails 5 - how to negate a method call on a before_save callback conditional

In my Template model I have this callback: before_save :set_status, if: :is_template? private def is_template? return self.template_type == 'template' end How can I change it so that it only fires when the template_type is NOT 'template'? I tried these: 1 before_save :set_status, if: !:is_template?...
rmcsharry
1

votes
2

answer
242

Views

Rails: how to exclude post requests for a before_action in the application controller

I have a rails app. I use a before_action in my application controller to get a variable for all identified users (I use it in the header so I need it for all my views). However, it slows down all my post requests which do not need the variable. I tried to find a way googling it but couldn't find an...
AlphaNico
1

votes
1

answer
488

Views

Unable to install vagrant plugin due to SSL_Connect error

Hello so I am trying to install a vagrant plugin but I getting the following error in mingW64 $ vagrant plugin install vagrant-winnfsd Installing the 'vagrant-winnfsd' plugin. This can take a few minutes... Vagrant failed to load a configured plugin source. This can be caused by a variety of issues...
GetBackerZ
1

votes
1

answer
300

Views

using ruby to extract email body from Microsoft graph rest api outlook message

I am fetching emails from Outlook through Microsoft Graph or Rest Api directly from Ruby without using any Microsoft SDK. The problem I am facing is that I can't get the email message body from the response returned by Microsoft. Using the Ruby Mail gem, I have tried to parse the response by doing:...
brg
1

votes
1

answer
332

Views

Webpacker creates huge node_modules

I created a new project with Rails 5.1 and I want to use webpacker to manage my react dependencies. The problem is after installing webpacker it creates a huge 130Mb+ dir node_modules with every possible node_package. This does not make sense as default behavior. How can I configure Webpacker to onl...
Leo Net
1

votes
1

answer
414

Views

How to turn off warnings, 'ruby -w', in 'rake test'?

I have inherited a huge Rails project with hundreds of warnings, due to other peoples' sloppy coding habits, none of which I can fix. When I run an individual test suite, with ruby test/function/my_controller_test.rb, I get a clean run with no warnings. But when I run rake test, something deep in Ra...
Phlip
1

votes
2

answer
1.1k

Views

docker-compose restart connection pool full

My team and I are converting some of our infrastructure to docker using docker-compose. Everything appears to be working great the only issue I have is doing a restart it gives me a connection pool is full error. I am trying to figure out what is causing this. If I remove 2 containers or (1 complete...
Ryan Condron
1

votes
1

answer
111

Views

What does the double-splat do in a method call?

While preparing for the Ruby Association Certified Ruby Programmer Exam, I was solving the prep test and came upon this scenario: def add(x:, y:, **params) z = x + y params[:round] ? z.round : z end p add(x: 3, y: 4) #=> 7 // no surprise here p add(x: 3.75, y: 3, round: true) #=> 7 // makes total se...
Upvote Me
1

votes
1

answer
332

Views

Rails / Webpack: TinyMCE cannot load skins (404 (Not Found))

I am working on a Rails project that has been using assets pipeline but we are currently trying to transition to webpack. I encountered a problem when attempting to get TinyMCE to work after pulling it through yarn - the text editor simply won't load. Before the transition to webpack Originally I us...
Joanna Gaudyn
1

votes
2

answer
163

Views

How to add item to json object ruby on rails?

I want to join items from 2 tables. There output: 'costs':[ { 'id':2, 'cost_name':'rent office', 'user_id':2, 'fix':true, 'amount':300300, 'created_at':'2018-11-05T18:36:19.108+06:00', 'updated_at':'2018-11-05T18:36:19.108+06:00' }, { 'id':3, 'cost_name':'new computer', 'user_id':2, 'fix':fals...
Arkhatinho
1

votes
2

answer
328

Views

File uploading with ActiveStorage in Vue.js

I'm looking to add file uploads to a Card model (including single and multiple files) using the new Rails 5.2 ActiveStorage feature. I'm stronger in Rails and am using Vue.js to pull this off - previously I would use Paperclip to handle this. However, I've setup the config/storage.yml file and neces...
VegaStudios
0

votes
1

answer
19

Views

save hash in database in ruby on rails

I'm trying to save a hash in my database but this does not work this is my table in my database create_table 'categories', force: :cascade do |t| t.string 'name', limit: 255 t.integer 'client_id', limit: 4 t.integer 'event_id', limit: 4 t.text 'color', limit: 65535 end in my model try...
Alex Cortes
1

votes
5

answer
69

Views

Concatenate a string if it is not blank

Is it possible in one line to print error_details only if it is not blank or nil? log_err('#{error_code} - #{error_details}') I guess I could set error_details = '' at the top. I am wondering if I can avoid that line of code.
userMod2
1

votes
3

answer
57

Views

Object must be a Date, DateTime or Time object. nil given

Object must be a Date, DateTime or Time object. nil given. Extracted source (around line #23): when I try to save with the blank date this error occurs, is there a strong textsolution where I can save without needing to fill it? EDIT: ===============================================...
1

votes
2

answer
50

Views

How to parse git diff of specific file using Ruby?

I have an Android XML (string) file that is edited in a Ruby script. I would like to list and output the changes that were made then. I have tried it with Nokogiri and nokogiri/diff. But it does not have the desired result. I also have the feeling that it has problems when a new line is added in the...
FrostX
1

votes
2

answer
53

Views

How to use AWS Document DB with Ruby On Rails? Compatibility with version 3.6?

recently AWS implemented compatibility with MongoDB version 3.6 via DocumentDB. Document DB requires a certificate that can be downloaded at: https://s3-us-gov-west-1.amazonaws.com/rds-downloads/rds-GovCloud-Root-CA-2017.pem Using a configuration file similar to: https://github.com/mongodb/mongoid/...
ipegasus
1

votes
2

answer
57

Views

How to display a list of multiple model

I'm working on a social networking media like Facebook. Users can create daily posts and events. In timeline, I want to display a combined list of @post and @event. Can I build that on Rails? How about this below in controller: @feed = Post.all + Event.all And I should divide each thumbnail view als...
k10a
1

votes
3

answer
44

Views

Display counted records from partial

I have a ruby application in which I have post and like models. I want to be able to click a like button and then refresh the number of likes for the post. For that I'm using ajax. My question is how I can count the likes for the post in my partial html document. At the moment the code in my _show....
flo3719
1

votes
1

answer
51

Views

How to scan text file for string, and run code if string isn't there?

I have tried: elsif file.grep(/Mode: 1/) puts 'test' else puts 'test but else' Codacy is saying this is wrong. Is there a way to improve this? According to Codacy: unexpected token kELSIF (Using Ruby 2.2 parser; configure using `TargetRubyVersion` parameter, under `AllCops`) unexpected token kELSE...
hadesnotatsujin
1

votes
2

answer
41

Views

how to call same module method as instance method and class method in ruby class

I want to call the same module method as a class method as well as an instance method in Ruby class. example module Mod1 def method1 end end class A end I want to call method1 like A.new.method1 or A.method1.
Bhimasen Rautaray
1

votes
2

answer
52

Views

rspec expect compare two hash that by excluding specified keys

Is there any spec expect which will compare two hashes by excluding specified keys alone. H1 = {'name' => 'XXXXx', 'age' => 29, 'DOB' => 'dd/mm/yyyy'} H2 = {'name' => 'XXXXX', 'age' => 29, 'DOB' => 'dd/mm/yyyy'} Compare the above two hashes by excluding DOB key alone.
Munish Prabhu
1

votes
1

answer
94

Views

Show age of user

I need to ask the user for their birth year, and then display every year from the birth year to 2017 with the age for each year. I started with this: puts 'when are you born (year) ?' birth_year = gets.to_i birth_year.upto(2017){ |birth_year| puts birth_year } I miss the part to display the age. I...
antoine
1

votes
3

answer
98

Views

error: The branch 'upgrading' is not fully merged

I recently cloned my application from Bitbucket and checkout to a new upgrading branch where I upgraded my application from rails 5.0.0.1 to rails 5.1.6: $ git checkout -b upgrading I firstly updated my Gemfile, the I ran bundle update: $ bundle update From the railsdiff website and from a new rails...
Asarluhi
1

votes
1

answer
54

Views

Validate uniqueness of rec ord with scope, is not working properly when saving nested records

I have a model User with a many-to-many relationship to Book, through UserBook. To create a new relationship, the user can either: Select from a list of available books Create a new book request by typing in ISBN code (which can be used to look up the book), they can submit multiple requests at the...
bo-oz
1

votes
2

answer
41

Views

Accessing an instance variable within the `initialize` method

Given this basic class in Ruby: class TestClass def initialize(name) @name = name end end How do I then access the instance variable name directly from within the initialize method without creating a getter function? is this even possible? (i.e. using dot notation) or does the initialize method ceas...
seeker
1

votes
2

answer
38

Views

Are there logs to see git commands from bundler?

I am using bundle install to install some Ruby gems from a mix of public and private git repos. The issue is that after one particular gem is installed through git, the local directory is polluted with that gem's source code. It's like the git repo has completely changed. Is there a way to log all t...
Rudolf Olah
1

votes
2

answer
39

Views

Ruby - automatically initialize class instance variables of child classes without inheriting the value

I am looking for a solution to automatically initialize a class variable through inheritance (make it available as an accessor and initialize it to some value). But I do NOT want to inherit the value, just start with a new fresh object each time on each class. I have been looking at class_attribute...
1

votes
1

answer
20

Views

delegate in rails active record

when digging into rails source code there are some some code in activerecord/lib/active_record/querying.rb QUERYING_METHODS = [ :find, :find_by, :find_by!, :take, :take!, :first, :first!, :last, :last!, :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!, :forty_two, :forty_two!,...
ThanksSirAlex
1

votes
3

answer
37

Views

How To Fix undefined method `product' for #<LineItem::ActiveRecord_Relation:0x0000000017b22f70>

I'm try product quantity - 1 but ı get this error line_item.rb belongs_to :order belongs_to :product payment.rb has_many :orders undefined method `product' for # LineItem::ActiveRecord_Relation:0x0000000017b22f70> @line_item = LineItem.where(:order_id => params[:zc_orderid]) @line_item.product.qua...
Mansur Berbero
1

votes
2

answer
25

Views

How to exclude items have already chosen

I'm working on the application which users can choose items but they can't choose ones have already been chosen. BoardItem has board_id and item_id. An item that was already chosen should not be included in the selected collection. Item and board have board items. @board = Board.find(params[:id]) @...
k10a
1

votes
2

answer
22

Views

Parsing a TimeZone name string that includes GMT offset hours

I did find this question but I am still stumbing around looking for a simple solution to the following: An API call returns the following format which looks like they are using Time.zone.to_s irb> ShopifyAPI::Shop.current.timezone => '(GMT-08:00) Pacific Time (US & Canada)' I would like to parse th...
kg.
1

votes
2

answer
80

Views

Returning duplicate characters in a string

I am learning Ruby. As part of my HW, I am to find the first occurence of two duplicate characters in a row in a string, and return the duplicated character. This is what I came up with: require 'set' def find_a_dup_using_set(arr) s = Set.new arr.find { |e| !s.add?(e) } end p find_a_dup_using_set(['...
Cholis
1

votes
2

answer
45

Views

How do I append/add data to an JSON file with Ruby

So I'm trying to add info to my .json file, a first name and a last name, from just running the script via terminal in kali linux. I have not have had much progress I have been searching online for the past 5 hours to find a way to fix this but there has not been much progress. Which leaves me her...
HorseLeg
1

votes
2

answer
18

Views

After save call back don't update

I am trying to set a callback for my Product Model, but the product attribute's discounted_price doesn't change... I can't call self.save it raises a stack level too deep What am I doing wrong? product.rb after_save :set_discount def set_discount self.discounted_price = self.price - (self.price * se...
johan
2

votes
0

answer
18

Views

How to do a negative lookbehind within a %r<…>-delimited regexp in Ruby?

I like the %r delimiters because it makes it really easy to spot the beginning and end of the regex, and I don't have to escape any /. But it seems that they have an insurmountable limitation that other delimiters don't have? Every other delimiter imaginable works fine: /(? it gives this syntax erro...
Tyler Rick
0

votes
2

answer
68

Views

Ruby DSL used across multiple <% %> tags or multiple lines

I am writing a helper DSL to make it easier to craft a nice menu ui within a view. The view's erb is producing an error undefined method 'safe_append=' for nil:NilClass when I break the block across multiple erb tags but it works fine if I stick it in one tag. I want to understand why -- it should...
Michael K Madison
1

votes
2

answer
1.6k

Views

Using accepts_nested_attributes_for on a join table with its own attributes - Duplicate rows

I have the following three models (Rails 2.3.8) class Outbreak < ActiveRecord::Base has_many :incidents, :dependent => :destroy has_many :locations, :through => :incidents accepts_nested_attributes_for :incidents, :allow_destroy => true accepts_nested_attributes_for :locations, :allow_destroy => tru...
Pasted
0

votes
0

answer
2

Views

Rails 6 + yarn + datatables issue

I bumped into troubles with webpacker. I'm using Rails 6.beta3 and trying to add Datatables to my app. My steps: yarn add datatables.net-dt then in app/javascript/packs/application.js: require('@rails/ujs').start() require('turbolinks').start() require('@rails/activestorage').start() require('channe...
snake
0

votes
0

answer
4

Views

Problem installing bundler, Says it installs, but then doesn't actually install

I am setting up a new system. I'm using rbenv instead of rvm because rvm changes the definition of 'cd' and that's just evil. I've got the required version of ruby and rails (I think) installed, but bundler is causing problems: turlingdrome$ gem install bundler ERROR: While executing gem ... (Errn...
Brian Postow

View additional questions