mercredi 17 juillet 2019

get record from many to many relationship

I am working on an old rails project. I have a many-to-many relationship between Student and Course via a bridge model named students_courses.

Student class:

class Student < ActiveRecord::Base
  has_and_belongs_to_many :courses, :class_name => 'Course', :join_table => 'students_courses'
end

Course class:

class Course < ActiveRecord::Base

end

StudentCourse class:

class StudentCourse < ActiveRecord::Base
  belongs_to :student
  belongs_to :course
end

There is a firstname attribute in Student model. I would like to find the student who has firstname "John" and studying course "foo". How can I get this in ruby of Rails?

mardi 16 juillet 2019

Surrogate character( 𩸽𠮟) from ruby on rails application gets saved to oracle database in question mark format (?)

Surrogate character( 𩸽𠮟) from ruby on rails application gets saved to oracle database in question mark format (?)

Ruby -2.4.1 Rails - 5.0 Oracle - 11.2

Using above configurations

firing below query

UPDATE table_name SET my_message = N'テストトト VP-𩸽𠮟' WHERE table_name.MERCHANT_ID = 'merchant4'  

While updating above record it gets saved to db as 'テストトト VP-??'

Please suggest.

Thank you in advance.

lundi 15 juillet 2019

content_tag populate drop-down with unknow "black squre" in front

I used the following code (in .slim file) to generate a dropdown. Each item in the dropdown is a hyperlink to other website. But I could not find out where does the black square ahead of each item is coming from. How to remove it?

enter image description here

li.dropdown
     a.dropdown-toggle data-toggle="dropdown" href="#"
         | Other 
         span.caret
     ul.dropdown-menu role="menu" 
        == content_tag :ul do items.map do |item| concat(content_tag(:li) do link_to(item, "#") end) end end

Minimagick error during upload "composite -compose Over -gravity North"

I am trying to upload an image by gem "carrierwave", "~> 1.1.0" and gem "mini_magick", "~> 4.7.0" to upload image. I am getting this error.

I am using this code to upload image.

  MiniMagick::Tool::Convert.new do |i|
    i.size "#{image_width.ceil}x#{watermarked_image_height.ceil}"
    i.gravity "center"
    i.xc "white"
    i << temp_image_path
  end
  @_watermarked_image = MiniMagick::Image.open(temp_image_path)
  @_watermarked_image = @_watermarked_image.composite(source_image, "jpg") do |c|
    c.compose "Over"
    c.gravity "North"
  end

:ERROR

composite -compose Over -gravity North /tmp/mini_magick20190710-1-g0dcpy /tmp/mini_magick20190710-1-1fil3d9.jpg /tmp/mini_magick20190710-1-ibbwos.jpg` failed with error:\\n

vendredi 12 juillet 2019

Faraday post request with Rails form

I am trying to make a post request from my controller to a third party website using faraday but I can't get it to work?

I have a method in my homes controller to make the request, a route setup for the method and a form for my request but I can't get the request to be successful no idea what i am doing wrong


    #homes_controller.rb
    def alpaca
        @conn = Faraday.post('https://paper-api.alpaca.markets/v2/orders') do |req|
          req.headers['APCA-API-KEY-ID'] = 'key'
          req.headers['APCA-API-SECRET-KEY'] = 'secret'
          req.body = "{'symbol': '#{params[:ticker]}'}"
          req.body = "{'qty': '#{params[:qty]}'}"
          req.body = "{'side': '#{params[:side]}'}"
          req.body = "{'type': '#{params[:ticker]}'}"
          req.body = "{'time_in_force': '#{params[:time_in_force]}'}"
        end
        redirect_to homes_path
      end

    #routes.rb
     resources :homes
      root 'homes#index'
      post '/alpaca', to: 'homes#alpaca'

    #homes/index.html.rb
     <%=form_tag alpaca_path do %>
     <%=label_tag "Ticker"%>
     <%=text_field_tag :ticker %>
     <%=label_tag "qty"%>
     <%=number_field_tag :qty %>
     <%=label_tag "side"%>
     <%=text_field_tag :side %>
     <%=label_tag "type"%>
     <%=text_field_tag :type %>
     <%=label_tag "time_in_force"%>
     <%=text_field_tag :time_in_force %>
     <%=submit_tag%>
     <%end%>


I am trying to get this form to post the params passed in sent to alpaca's trading api

how to fix undefined method 'each ' for nil:class

I need some help about this error.i check evrything such mi index,and my controller but i still get the same error :NoMethodError in Articles#index undefined method 'each' for nil;class

<% @articles.each do | article | %>

  <td><%= article.title %></td>

Generate uuid in size of 16 and datatype : binary

To generate uuid for 16bit binary datatype in rails

I have used this 'SecureRandom.uuid' but it takes very big size. It takes big size, and i have binary(16) datatype and size, database=mysql; uuid = SecureRandom.uuid

I want 16 sized uuid and store in binary datatype.