Method: Sequel::Dataset#set_graph_aliases
- Defined in:
- lib/sequel/dataset/graph.rb
#set_graph_aliases(graph_aliases) ⇒ Object
This allows you to manually specify the graph aliases to use when using graph. You can use it to only select certain columns, and have those columns mapped to specific aliases in the result set. This is the equivalent of select for a graphed dataset, and must be used instead of select whenever graphing is used.
graph_aliases should be a hash with keys being symbols of column aliases, and values being either symbols or arrays with one to three elements. If the value is a symbol, it is assumed to be the same as a one element array containing that symbol. The first element of the array should be the table alias symbol. The second should be the actual column name symbol. If the array only has a single element the column name symbol will be assumed to be the same as the corresponding hash key. If the array has a third element, it is used as the value returned, instead of table_alias.column_name.
DB[:artists].graph(:albums, :artist_id: :id).
set_graph_aliases(name: :artists,
album_name: [:albums, :name],
forty_two: [:albums, :fourtwo, 42]).first
# SELECT artists.name, albums.name AS album_name, 42 AS forty_two ...
244 245 246 247 248 249 250 251 |
# File 'lib/sequel/dataset/graph.rb', line 244 def set_graph_aliases(graph_aliases) columns, graph_aliases = graph_alias_columns(graph_aliases) if graph = opts[:graph] select(*columns).clone(:graph => graph.merge(:column_aliases=>graph_aliases.freeze).freeze) else raise Error, "cannot call #set_graph_aliases on an ungraphed dataset" end end |