vendredi 31 juillet 2015

Hash and values

I came across this Ruby script:

frequency = Hash.new(0)
...
...
file.read.downcase.scan(/\b[a-z]{4,20}\b/){|word| frequency[word] =
frequency[word]+1}

The point I couldn't understand is frequency[word] = frequency[word]+1

Wouldn't frequency[word] give me the word matched? How can we add it to 1?

Mongoid, setting custom accessor field in mongoid-history gem

I'm adding mongoid-history gem to my project.

According to guide in github, when I add Userstamp to my tracker it creates created_by field with accessor called creator.

They have written that I can rename it via gem config.

How to rename this field?

Rails_admin show child models fields in parents show page

I have Item model and I have ItemImage child model which is embedded inside of its parent.

When I open single Items page in rails_admin, it shows all of its children like this:

ItemImage #55b766556c656e12d0ac0000, ItemImage #55b766556c656e12d0ad0000, and ItemImage #55b766566c656e12d0ae0000

How to make rails_admin to show all of its children fields inside parents page? Is it possible to show them as table?

I'm using Mongoid.

How best to represent roles in the DB of a rails app?

I'm building authorization in my app and have three roles:

  • User
  • Moderator
  • Admin

What's the best way to represent this in Rails? I've thought about adding the following boolean fields to my user's table:

  • is_user
  • is_moderator
  • is_admin

And then creating is_user?, etc functions in the User model.

A user can have multiple roles (can be all three).

Can't Compile LESS to CSS with LESS2CSS Plugin on Sublime Text 3 Mac

I am using Sublime Text 3 on Mac OS Yosemite. I want to use LESS2CSS package but it not working. Here are the errors it gives:

WARNING] Please install gem 'therubyracer' to use Less.
/Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- v8 (LoadError)
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less/java_script/v8_context.rb:2:in `<top (required)>'
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less/java_script.rb:9:in `default_context_wrapper'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less/java_script.rb:17:in `context_wrapper'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less/loader.rb:13:in `initialize'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less.rb:14:in `new'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less.rb:14:in `<module:Less>'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less.rb:9:in `<top (required)>'
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/bin/lessc:3:in `<top (required)>'
    from /usr/bin/lessc:23:in `load'
    from /usr/bin/lessc:23:in `<main>'

I've installed Node.js and installed less, but it is still not working.

Cannot find my mistake with jQuery autocomplete and Rails 4

router

  resources :pcps  do
    collection do
      get 'autocomplete'
    end
  end

controller

def autocomplete
    @pcps = Pcp.order(:last).where("last like ?", "%#{params[:term]}%")
    render json: @pcps.map(&:full_name)
end

View

= f.text_field :pcp, data: {autocomplete_source:  '/pcps/autocomplete'}

Coffee script

This code works

jQuery ->
  $('#sched_pcp').autocomplete
    source: ["Atest", "Btest","Ctest","Dtest","Btest1","Btest2"]

This code does not work.

jQuery ->
  $('#sched_pcp').autocomplete
    source: $('#sched_pcp').data('autocomplete-source')

I am using ruby 2.2 and rails 4.2 The url '/pcps/autocomplete' returns the correct data. Can someone help me see my mistake please? or maybe give me an idea how to debug it.

How do I seed a unique record ONLY via my seeds.db?

I have the following code in my seeds.rb used to create a record in my basic Rails app via seeding.

Post.create( title: "Unique Title!", body: "this is the most amazingly unique post body ever!" )

When running the rake db:seed command, it obviously seeds the db with this data. My question is how do I add a check or safeguard in the code so that it only enters that once, i.e. as a unique? If I rerun rake db:seed, I don't want add that same entry again.

Thanks