Posts

Showing posts with the label ruby

Can't find jekyll

Can't find jekyll Jekyll was running just fine a couple days ago. I can't figure out what changed or understand the error. How can I fix this mess probably caused by me? jekyll -v Traceback (most recent call last): 2: from /usr/local/bin/jekyll:23:in `<main>' 1: from /usr/local/lib/ruby/site_ruby/2.5.0/rubygems.rb:308:in `activate_bin_path' /usr/local/lib/ruby/site_ruby/2.5.0/rubygems.rb:289:in `find_spec_for_exe': can't find gem jekyll (>= 0.a) with executable jekyll (Gem::GemNotFoundException) Here's my setup on Mac 10.13.5 High Sierra: Computer:~ me$ brew -v Homebrew 1.6.9 Homebrew/homebrew-core (git revision 69b9; last commit 2018-06-29) Computer:~ me$ brew list autoconf libyaml pkg-config readline ruby-build hugo openssl rbenv ruby Computer:~ me$ which ruby /usr/local/bin/ruby Computer:~ me$ which gem /usr/local/bin/gem bundle -v Bundler version 1.16.2 Computer:~ me$ ruby -v ruby 2.5.1p57 (2018-03-29 revision 630...

How to find instance of an object in a collection within a collection in rails

How to find instance of an object in a collection within a collection in rails I'm trying to write an if statement that will find an instance of an object in a collection within another collection... House has_many :occupants Occupant has_many :shirts belongs_to :house Shirt belongs_to :occupant So if I want check if any of the occupants of house own a white shirt, I want to do something like this: <% if @house.occuptants.shirts.where(:color => 'white') %> However when I do that I get an error: undefined method `shirts' for #< Occupant::ActiveRecord_Associations_CollectionProxy I believe because occupants is a collection in this case, but I'm not sure what the correct approach or syntax should be. 2 Answers 2 Something simpler and that will help you later for different use cases is to add more to the relationships: class House has_many :occupants has_many :shirts,...

Why is character in string not mutated?

Why is character in string not mutated? def test_problem(str) str[3].upcase! # str[3] = str[3].upcase! works str end p test_problem("hello") My question is why String.upcase! which is mutating method doesn't mutate string in a case above but you need to reassign that character in string? 2 Answers 2 String# returns a new string, as is documented. String# a = "foo" a.object_id # => 70217975553640 a[0].object_id # => 70217957574840 A string is not composed of character objects, it is a single object (at least on a superficial level, I'm not sure of the C internals). So there is no way to extract a character and still have it belong to the original string - you need to work with the string as a whole if you want to mutate it. String#= on the other hand does mutate the string String#= You could make your method like so: def test_problem(str) str[3] = str[3].upca...

In Ruby, why is it not enough to alias `method_missing` to catch call to undefined methods?

In Ruby, why is it not enough to alias `method_missing` to catch call to undefined methods? That seems all the more unexpected when defining a dummy method passing all arguments do the job. That is the following works: def method_missing(ago, *lokatoj, &bloko) mistrafe(ago, *lokatoj, &bloko) end def mistrafe(ago, *lokatoj, &bloko) # faru ion end While the following doen't alias mistrafe method_missing Why is that so? 1 Answer 1 alias mistrafe method_missing does not work because the syntax of alias is: alias mistrafe method_missing alias alias OLD_NAME NEW_NAME That said, the following will work: alias method_missing mistrafe By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to th...

Ruby and Rails: Show login form content of one view into the another view files modal popup

Ruby and Rails: Show login form content of one view into the another view files modal popup I want to open modal on button click of the coursedetail.html.erb the file which is done using a jquery modal. But now I want to show my login.html.erb file content in this modal so that user will be able to login directly from the modal login form. coursedetail.html.erb modal Put your login in a partial and render it in the modal. – Marc Talbot Jun 29 at 10:49 1 Answer 1 You can you partial to render the view in rails. To know more about partial click here partial By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your ...

Finding posts under a category name keyword with pg_search on Rails 5

Finding posts under a category name keyword with pg_search on Rails 5 I'm using gem pg_search and doing multi-search for Website and Post tables. I am able to search by records title, but I also want to be able to search records based on categories they belong to. pg_search So, for example, when I type "typographic" (category name) to search input, all Website and Post records under that category should be listed, as well as all records that have "Typographic" in the title. I think it can be done with association but I could not make it work 🤷‍♂️ _header.html.erb <%= form_tag search_index_path, method: :get do %> <%= text_field_tag :query, params[:query], placeholder: "Search" %> <% end %> search_controller.rb class SearchController < ApplicationController def index @pg_search_result = PgSearch.multisearch(params[:query]) end end models/post.rb class Post < ApplicationRecord include PgSearch multisearchable :ag...

ArgumentError: wrong number of arguments (given 0, expected 2..3)

ArgumentError: wrong number of arguments (given 0, expected 2..3) I'm trying to get searchkick to work but I run into an error when I attempt to reindex my stories via the console. I can't seem to understand what the error might be and would love some help. The error via the console after entering "Story.reindex": Story Import (3.6ms) {"count":2,"exception":["ArgumentError","wrong number of arguments (given 0, expected 2..3)"],"exception_object":"wrong number of arguments (given 0, expected 2..3)"} ArgumentError: wrong number of arguments (given 0, expected 2..3) from app/models/story.rb:42:in `hot_score' from (irb):1 story.rb model: class Story < ApplicationRecord searchkick has_many :votes scope :newest, -> { order(created_at: :desc) } scope :hottest, -> { order(hot_score: :desc) } def upvotes votes.sum(:upvote) end def downvotes votes.sum(:downvote) end def calc_hot_score points =...