ActiveRecord union with Arel
I had to create a complex SQL query with rails that must return an ActiveRecord::Relation
After reading over Dan Shultz presentation about Arel i decided to follow the solution he provided.
beer = Beer.arel_table
union = Beer.where(name: "Oberon") \
.union(Beer.where(name: "Two Hearted"))
Beer.from(beer.create_table_alias(union, :beers).to_sql)
so only thing i need to add is the .to_sql
call (This was needed for Arel 3.0.2 for sure)
Batmanjs Custom View
class App.AutocompleteView extends Batman.View
html: "<input type='text' />"
autocompleteSource: -> []
viewDidAppear: ->
# I had to replace @node (as it is in the official documentation) to @node.firstChild since that is <input type='text' />
# @node in my experiment is <div class="large-12 columns>
$(@node.firstChild).autocomplete
source: @autocompleteSource()
HTML template:
</...
Asssign Batmanjs model option to a select element
Given the following js model:
class MyApp.Post extends Batman.Model
@encode options
Given the server side json (generated with rabl):
object @post
attributes :id, :category
node(:categories) {
Category::OPTIONS.map{ |option| { id: option, name: I18n.t(option) } }
}
And finally the binded view (haml):
# This binds the selected value to the model object
%select{ "data-bind" => "post.category" }
# Iterate over categories
%option{ "data-foreach-category" =>"post.categories",
...
Batmanjs Model attribute reader
$context($0).controller.model_name.get('attribute_name')
Debug batamanjs model in view context
To inspect a model attributes binded to a dom element
$context($("#elem-id")[0]).controller.model_name.toJSON()
Original idea taken from the link below.