[Rails] ActiveRecord: preloading on relations with a select clause

2015-11-30 Thread alexander . maznev


Hello,
It turns out that our Apps Variant relation model + ActiveRecord Relations 
produces some very funky behaviour which was preventing it from being used in 
.includes preloads.

Background: Our App's variant model has a boolean column(composite) and they 
reflect back on themselves through a subsidiary table
(composites). A Composite entry may reference a variant as a variant_id which 
has many other composite variants which reference the former by parent_id. e.g.

Variants
id: 1
name: SnackBox
composite: true

id: 2
name: Snickers
composite: false 

id: 3
name: Bounty
composite: false 

Composites
id: 1 # references SnackBox
parent_id: null # is a parent
variant_id: 1 

id: 2 #references snickers a composite of SnackBox
parent_id: 1
variant_id: 2 

id: 3 #references bounty a composite of SnackBox
parent_id: 1
variant_id: 3

Here is the original code we used in the App to achieve loading this relation:

>
> class Variant < ActiveRecord::Base
>  has_many :composites, foreign_key: :parent_id, dependent: :destroy
>  has_many :composite_variants_singular, -> { select "variants.*, 
> composites.quantity as composite_quantity" },
>  through: :composites, source: :variant
> end
>
>
Here is what happens when you run the composite_variants on a single variant.

Variant Load (0.5ms) SELECT "variants".* FROM "variants" WHERE "variants"."id" 
= $1 LIMIT 1 [["id", ]]
>
>
And alternatively in an includes (notice that SELECT variants.* will fail in 
the later):

Variant Load (0.7ms) SELECT variants.*, composites.quantity as 
composite_quantity FROM "variants" 
INNER JOIN "composites" ON "variants"."id" = "composites"."variant_id" 
WHERE "composites"."parent_id" = $1 [["parent_id", ]]
Here is the alternative code path I attempted: 
>
> has_many :composite_variants_joined, -> {
>  joins('''INNER JOIN "composites" ON "composites".variant_id = 
> "variants".id''')
>  .select "variants.*, composites.quantity as composite_quantity"
> },
> through: :composites, source: :variant
>
> The alternative query generated looks like this (which works correctly for 
includes): 
>
> Variant Load (89.5ms) SELECT "variants".* FROM "variants" WHERE "variants"
> ."account_id" = $1 [["account_id", ]]
> Composite Load (53.6ms) SELECT "composites".* FROM "composites" WHERE 
> "composites"."parent_id" IN ()
> Variant Load (3.0ms) SELECT variants.*, composites.quantity as 
> composite_quantity FROM "variants" INNER JOIN "composites" ON "composites"
> .variant_id = "variants".id WHERE "variants"."id" IN ()
>
> But breaks tremendously on the single instance load. 
Variant Load (0.4ms) SELECT "variants".* FROM "variants" WHERE "variants".
"id" = $1 LIMIT 1 [["id", ]]
PG::DuplicateAlias: ERROR: table name "composites" specified more than once
: SELECT variants.*, composites.quantity as composite_quantity FROM 
"variants" INNER JOIN "composites" ON "variants"."id" = "composites".
"variant_id" INNER JOIN "composites" ON "composites".variant_id = "variants"
.id WHERE "composites"."parent_id" = $1 

The final solution I ended up with looked like this: 
>
> class Variant < ActiveRecord::Base
>  has_many :composites, foreign_key: :parent_id, dependent: :destroy
>  has_many :parent_composites, class_name: "Composite"
>  has_many :composite_variants, ->(variant) { _composite_variants(variant) 
> },
>  through: :composites, source: :variant 
>
>  def self.with_composite_quantity
>   self.select("variants.*, composites.quantity as composite_quantity")
>  end 
>  # This is a work-around to allow fetching composite variants for an 
> single variant instance
>  # as well pre-loading of of composite_variants via .includes - Since AR 
> does not support a single
>  # method for doing so we determine which method to use based on whether 
> the lambda has a variant
>  # instance passed in as an argument.
>  def self._composite_variants(variant)
>   if variant
>with_composite_quantity
>   else
>self.joins(:parent_composites).with_composite_quantity
>   end
>  end
> end
>
> While I don't expect ActiveRecord Relations to handle every single use 
case - the clearly incorrect SQL (duplicating the same INNER JOIN clause 
twice) on the .includes() and the complexity of the final solution is quite 
suspect, so I was hoping to have an outside opinion. Thanks for your time. 
Alex 


P.S. Sorry about the formatting the Google UI is wrapping all my attempts to 
format code-blocks into single lines without the quotes. 


-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/03851141-8e03-42c5-b46a-f504cbe18283%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] jquery change function

2015-11-30 Thread fugee ohu
this snippet which follows another code block under   
$(document).ready(function () {isn't working, the previous block works, 
but not this one

  $('#listing_free_shipping').change(function(e) 
   {
if (e.listing_free_shipping.attr('checked')) 
 {
 $("#listing_shipping_cost").toggle( "blind" );
 }
else
 {
 $("#listing_shipping_cost").show();
 }
  })

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/b61f22f6-cd32-4dd7-9f53-e4eaf637a523%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] ANN - Invoker 1.4.1 released

2015-11-30 Thread Hemant Kumar
Hello folks,

Team Codemancers is proud to release Invoker version 1.4.1. If you haven't
used Invoker before, checkout http://invoker.codemancers.com/ for more
information.

This new version packs bunch of goodies and fixes. A brief list is as
follows:

1. Invoker supports processes that are inactive by default but can be
started
   via invoker add. More information -
https://github.com/code-mancers/invoker/pull/114

2. Invoker now supports a global config file defined in ~/.invoker
directory which can be
   used from anywhere.

3. Added support for Debian, Mint and Arch Linux.

4. When starting invoker one can specify the beginning port. More details:
   https://github.com/code-mancers/invoker/pull/147

5. Added bash completion for Invoker -
https://github.com/code-mancers/invoker/pull/148

6. when running invoker list, also show port number -
https://github.com/code-mancers/invoker/pull/149

7. Invoker now supports different subdomains to be directed to different
applications.
   For example: foo.dev can direct to web application whereas chat.foo.dev
can direct to
   another application. More details -
https://github.com/code-mancers/invoker/pull/129



If you have any feedback or comments, please let us know.



-- 
Cheers
Hemant
Co founder - http://www.codemancers.com
http://twitter.com/gnufied

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CABQG7ZtCfq9dqE%3DjSiPasfjtN%3D%3DFE9bAGRcevVb2hhxKxjC8sA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Grib_api.h could not be found while installing grib api

2015-11-30 Thread Colin Law
On 30 November 2015 at 21:22, Colin Law  wrote:
> On 30 November 2015 at 19:26, Yash Narwal  wrote:
>> Hey mate thanks for heads up, your suggestion worked in installing the gem.
>> But how can I use this gem now, like if I follow this example , it gives me
>> no such file can not be loaded 'numru/grib'. So is there any other docs
>> which should I follow to use this gem ?
>
> I have no idea, I have not heard of it before you asked, I just
> googled a bit to come up with my previous suggestion.  However if you
> tell us more someone may be able to help.  Post the source file around
> the area giving the error and post the full error message and tell us
> which line it is failing on.

On second thoughts I assume you are getting the error on the line
require 'numru/grib'.  Is this in a Ruby on Rails app or in a straight
ruby script?

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsdBjcfL0AVTyAEs9mJWHnao5RbCsch9K5oiXv%3DADXyMw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Grib_api.h could not be found while installing grib api

2015-11-30 Thread Colin Law
On 30 November 2015 at 19:26, Yash Narwal  wrote:
> Hey mate thanks for heads up, your suggestion worked in installing the gem.
> But how can I use this gem now, like if I follow this example , it gives me
> no such file can not be loaded 'numru/grib'. So is there any other docs
> which should I follow to use this gem ?

I have no idea, I have not heard of it before you asked, I just
googled a bit to come up with my previous suggestion.  However if you
tell us more someone may be able to help.  Post the source file around
the area giving the error and post the full error message and tell us
which line it is failing on.

Colin

>
> I really appreciate your help.
>
> Thanks
> Yash
>
> On Sunday, November 29, 2015 at 8:28:05 PM UTC+11, Colin Law wrote:
>>
>> On 29 November 2015 at 08:33, Yash Narwal  wrote:
>>>
>>> HI there , I am trying to install a ruby gem called rb-grib but I am
>>> constantly having issues while doing so.
>>> I am using Ubuntu 15.04
>>>
>>> So  I went to this page http://www.rubydoc.info/gems/rb-grib/0.2.2
>>> I tried to install requirements for this gem which are
>>>
>>> Ruby (www.ruby-lang.org/) Pass
>>> NArray (narray.rubyforge.org/index.html.en) Pass
>>> NArrayMiss (ruby.gfd-dennou.org/products/narray_miss/) Pass
>>> GRIB API (www.ecmwf.int/products/data/software/grib_api.html) Fail
>>>
>>> In order to install grib_api I followed every instructions I can find on
>>> web but when it comes to final step in installation (sudo make install) , it
>>> gives me an error
>>> "checking for grib_api.h ... no"
>>> "Could not create makefile due to some reason"
>>>
>>> I have followed this guide to install grib api Guide
>>> Snapshot of the error I am having in the final command for the api(from
>>> the guide link) (make install)
>>> I am really look forward to have some help in installing the rb-grib gem.
>>
>>
>> sudo apt-get install ruby-grib
>> might be what you want.  The description is "Ruby interface to the ECMWF
>> GRIB API
>> It is always worth using synaptic to search the repositories before trying
>> to install by hand.
>>
>> Colin
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/f5c737b3-563e-4d20-93b9-b5f3fbdd997c%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLt89suFvKXMeEHNCrJp1%3D6-TxTOqoq_oKV-mpt8oqUDtQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Grib_api.h could not be found while installing grib api

2015-11-30 Thread Yash Narwal
Hey mate thanks for heads up, your suggestion worked in installing the gem. 
But how can I use this gem now, like if I follow this example 
 , it gives me no such 
file can not be loaded 'numru/grib'. So is there any other docs which 
should I follow to use this gem ?

I really appreciate your help.

Thanks
Yash

On Sunday, November 29, 2015 at 8:28:05 PM UTC+11, Colin Law wrote:
>
> On 29 November 2015 at 08:33, Yash Narwal 
> > wrote:
>
>> HI there , I am trying to install a ruby gem called rb-grib but I am 
>> constantly having issues while doing so.
>> I am using Ubuntu 15.04
>>
>> So  I went to this page http://www.rubydoc.info/gems/rb-grib/0.2.2
>> I tried to install requirements for this gem which are 
>>
>>- Ruby (www.ruby-lang.org/) Pass
>>- NArray (narray.rubyforge.org/index.html.en) Pass
>>- NArrayMiss (ruby.gfd-dennou.org/products/narray_miss/) Pass
>>- GRIB API (www.ecmwf.int/products/data/software/grib_api.html) Fail
>>
>> In order to install grib_api I followed every instructions I can find on 
>> web but when it comes to final step in installation (sudo make install) , 
>> it gives me an error
>> "checking for grib_api.h ... no"
>> "Could not create makefile due to some reason"
>>
>> I have followed this guide to install grib api Guide 
>> 
>> Snapshot of the error I am having in the final command for the api(from 
>> the guide link) (make install)
>> I am really look forward to have some help in installing the rb-grib 
>> gem.
>>
>
> sudo apt-get install ruby-grib
> might be what you want.  The description is "Ruby interface to the ECMWF 
> GRIB API
> It is always worth using synaptic to search the repositories before trying 
> to install by hand.
>
> Colin
>  
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/f5c737b3-563e-4d20-93b9-b5f3fbdd997c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] jquery blind effect not being applied

2015-11-30 Thread fugee ohu

$(document).ready(function () {
  $('#listing_format').change(function(e) 
   {
if (e.target.value=('Fixed price') {$("#listing_starting_bid").toggle( 
"blind" ); }
  })
})


<%= form_for(@listing) do |f| %>
  <% if @listing.errors.any? %>

  <%= pluralize(@listing.errors.count, "error") %> prohibited this 
listing from being saved:

etc ...

I want the document above to apply the blind effect to the starting bid 
field but nothing changes in the browser


-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/29b6fc16-0d5e-42d9-af43-5416c433556c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Failure to install or update any gem through Win CMD

2015-11-30 Thread Colin Law
On 30 November 2015 at 13:19, Alexis Cretton  wrote:
> Hello,
>
> I've been trying to install and update different gems to work with Ruby
> on Rails, but keep getting the same error, apparently indicating I can't
> install or update any gem:
>
> I generally type in something like:
>
> gem update --system or gem install Rails --version 4.0.0 --no-ri
> --no-rdoc
>
> And get the following:
>
> ERROR: While executing gem ... (Errno::EINVAL)
> Invalid Argument - socket(2) - udp
>
> Note that I've proceeded to the same setup just a couple of days before
> on a different machine and had no problem.

Are you running the same version of Ruby on both setups, and which version is it
ruby -v
will tell you.

You may well have difficulties getting help with a Windows setup
however, as few Rails developers use Win.

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsJDHzG8hyVBD9dEhFK-5u-NuBe9-rgA4Npo5OWXtR81A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] Re: Some tracing guideance appreciated to understand how Ruby registers javascript in Rails

2015-11-30 Thread Frederick Cheung

On Sunday, November 29, 2015 at 4:09:04 PM UTC, dave wrote:
>
> Fred 
>>
>  
> Hi Fred,
> Thank you for your response and explanation: development vs production 
> javascript tags. Yes I also set traces into sprockets and manifest.rb code 
> before my original post.
> Where the tracing falls down is in the eval statement which switches into 
> another run context and so Byebug is left hanging.
> Maybe this is a Byebug forum question? or possibly i'm pushing its 
> run-context envelope past its original design?
>
>>
>>
Given that the eval calls Rack::Builder.new I would set a breakpoint on 
 Rack::Builder.initialize. I don't think you need any tools beyond bye bug 
for this sort of thing. However, this won't lead you directly to where 
javascript gets generated because that is done on demand, not at app 
startup. You'd want a breakpoint at the top of the rack middleware if you 
wanted to trace what happens when a js file is requested.

Fred
 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/0917c395-c7d6-49e5-bd30-1e775f22908c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] Failure to install or update any gem through Win CMD

2015-11-30 Thread Alexis Cretton
Hello,

I've been trying to install and update different gems to work with Ruby
on Rails, but keep getting the same error, apparently indicating I can't
install or update any gem:

I generally type in something like:

gem update --system or gem install Rails --version 4.0.0 --no-ri
--no-rdoc

And get the following:

ERROR: While executing gem ... (Errno::EINVAL)
Invalid Argument - socket(2) - udp

Note that I've proceeded to the same setup just a couple of days before
on a different machine and had no problem.

I couldn't figure any place where this same Error was explained or
encoutered.

Anybody here can help out?

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/46b502f97c68c73f6d8eea0952061997%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] categories

2015-11-30 Thread tonypm
Several years ago, I had a similar desire to have a screen representation 
of a category tree.  I was very surprised that I couldn't find any 
pre-baked solution, so I built my own (there may be one out there now of 
which I am unaware).  Unfortunately, I have never got round to making my 
code public, but I can share some of the design considerations I had to 
consider.

1.  What sort of tree would I like - I went for creating something along 
the lines of the windows file explorer - a pretty standard representation.
2.  What elements would this require:
a.  Database - a category structure - ie.  Awesome nested set worked well 
for me.
b.  Visual - simple graphics, small images for: node open, node closed, leaf
c.  Next step was to build  a simple mock up on the screen to create the 
CSS for the tree structure.  HTML struture would be nested ul lists for 
branches and li elements for nodes and leafs.  classes for the open and 
closed state.  (I could share this if you like)

2.  Perhaps the most difficult question is the decision of how much I am 
going to do in JS and how much server side.  I considered supplying the 
whole tree in json format and then using js to build the visual and keep 
things in step.  But it just seemed to get too involved.  And took me more 
into js development than I was comfortable with.

3.  The solution I used was to maintain the tree on the server, and open 
close, create and delete nodes using ajax calls.  Rewriting sections of the 
tree by replacing nodes by rendering appropriate partials.

4.  Giving each node an id of the category model object id allows the 
server to manage the tree by js responses that show/hide branches.

5.  It can seem a bit of a daunting task, but if you break it down into the 
steps above, create open/closed partials that call each other, then build 
the ajax calls for open/close,  it is actually not that hard, and I have a 
working solution that is common across three projects. Rails 2 and Rails 
4.  


Hopefully this at least gives some food for thought.
Tony



On Monday, 23 November 2015 21:13:57 UTC, Colin Law wrote:
>
> On 23 November 2015 at 21:11, fugee ohu > 
> wrote: 
> > how to format them on a page ... my thought was to use group_by for 
> > formatting a page that displays all categories 
>
> Do the tutorial. 
>
> Colin 
>
> > 
> > On Monday, November 23, 2015 at 3:39:05 PM UTC-5, Colin Law wrote: 
> >> 
> >> On 23 November 2015 at 20:11, fugee ohu  wrote: 
> >> > I'm looking for a solution for managing categories My categories 
> table 
> >> > has 
> >> > name , parent_id fields My first challenge is to display all 
> categories 
> >> > on a 
> >> > single page and my next is to build select lists represenative of the 
> >> > tree 
> >> > (after this has been done I wanna  integrate listings with them) 
> >> 
> >> I don't understand what is challenging about your first challenge.  Do 
> >> you mean you don't know how to get the categories from the database 
> >> (Category.all) or how to format them on a page or what? 
> >> 
> >> It suggests to me that you have still not followed my and Tamara's 
> >> advice to work right through railstutorial.org.  Until you understand 
> >> the basics you are just wasting your time and ours. 
> >> 
> >> Colin 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "Ruby on Rails: Talk" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to rubyonrails-ta...@googlegroups.com . 
> > To post to this group, send email to rubyonra...@googlegroups.com 
> . 
> > To view this discussion on the web visit 
> > 
> https://groups.google.com/d/msgid/rubyonrails-talk/145b7f8d-6956-44b3-ac9a-57fecd95e33e%40googlegroups.com.
>  
>
> > 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/384f64e3-ae98-4ce7-a9d7-b1bda61ec04e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.