Beginner Error in Rails: confusing an attribute of ActiveRecord instanze with an instance variable
As Ruby programmer you are used to write code like this, if you want to have a default value or rather make sure, that something is an Array or Hash, in order to rely on that for later treatment
def filters
@filters ||= { }
end
In Active Record that does not work, cuz:
@query.instance_variable_get :@filters
=> nil
@query.attributes
=> {"name"=>"_", "column_names"=>nil, "group_by"=>nil, "project_id"=>1, "sort_criteria"=>nil, "user_id"=>0, "search_mode"=>"all", "filters"=>{:subproject_id=>{:value=>[], :operator=>"*"}}, "is_public"=>false}
there is also obie's great post about this: http://www.jroller.com/obie/entry/default_values_for_activerecord_attributes
pry - alternative to irb and making debugging any ruby application (including rails) fun
Definitely check out this railscasts: http://railscasts.com/episodes/280-pry-with-rails
- walk into ruby objects ( cd Array; self # => Array; cd new; self #=> [] )
pry(main)> cd Array pry(Array):1> self => Array pry(Array):1> cd new pry([]):2> self => []
- ls -m shows the current objects methods
pry(main)> cd Array pry(Array):1> ls -m [:[], :allocate, :new, :superclass, :try_convert, :yaml_tag]
reload-method reloads the file containing the method specified, edit-methods open up your code editor with the file containing the method (cursor at the correct method) thus, when you use pry for debugging, you can play with a method, edit it, reload it, play with it again.
help lists all available commands :)
Command List: -- help This menu. install-command Install a disabled command. toggle-color Toggle syntax highlighting. simple-prompt Toggle the simple prompt. pry-version Show Pry version. import-set Import a command set reload-method Reload the source file that contains the specified method reset Reset the REPL to a clean state. ri View ri documentation. e.g `ri Array#each` show-doc Show the comments above METH. Type `show-doc --help` for more info. Aliases: ? stat View method information and set _file_ and _dir_ locals. Type `stat --help` for more info. gist-method Gist a method to github. Type `gist-method --help` for more info. gem-install Install a gem and refresh the gem cache. gem-cd Change working directory to specified gem's directory. gem-list List/search installed gems. (Optional parameter: a regexp to limit the search) ls Show the list of vars and methods in the current scope. Type `ls --help` for more info. cd Start a Pry session on VAR (use `cd ..` to go back and `cd /` to return to Pry top-level) nesting Show nesting information. jump-to Jump to a Pry session further up the stack, exiting all sessions below. exit End the current Pry session. Accepts optional return value. Aliases: quit, back exit-all End all nested Pry sessions. Accepts optional return value. Aliases: !!@ exit-program End the current program. Aliases: quit-program, !!! !pry Start a Pry session on current self; this even works mid-expression. whereami Show the code context for the session. (whereamishows extra lines of code around the invocation line. Default: 5) ! Clear the input buffer. Useful if the parsing process goes wrong and you get stuck in the read loop. show-input Show the contents of the input buffer for the current multi-line expression. amend-line Amend a line of input in multi-line mode. Type `amend-line --help` for more information. Aliases % play Play back a string or a method or a file as input. Type `play --help` for more information. hist Show and replay Readline history. Type `hist --help` for more info. Aliases: history . All text following a '.' is forwarded to the shell. shell-mode Toggle shell mode. Bring in pwd prompt and file completion. cat Show output of file FILE. Type `cat --help` for more information. show-method Show the source for METH. Type `show-method --help` for more info. Aliases: $, show-source show-command Show the source for CMD. Type `show-command --help` for more info. edit Invoke the default editor on a file. Type `edit --help` for more info edit-method Edit a method. Type `edit-method --help` for more info.
Thanks!
Documenting the Redmine API
DocumentationAPI
grep -rn --color --exclude-dir 'config/locales' --exclude-dir 'vendor' --exclude-dir 'public' --exclude-dir 'log' 'api' .
SETTINGS
THE WORKFLOW
- > Mime Type 'api' registered
- > respond_to.api
- > Template Handler 'api' registered
- > Builder.for(params[:format])
Supported Formats
- json
- xml
The global api - methods
./config/initializers/10-patches.rb:99: def api(&block)
./app/helpers/application_helper.rb:922: def api_meta(options)
Including Relations
./app/helpers/application_helper.rb:911: def include_in_api_response?(arg)
Where is it
Building the Response
/lib/redmine/views/api_template_handler.rb:24
The authentication system
User
/app/models/user.rb:52: has_one :api_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='api'"
./app/models/user.rb:246: def api_key
./app/models/user.rb:247: token = self.api_token || self.create_api_token(:action => 'api')
./app/models/user.rb:294: def self.find_by_api_key(key)
API enabled controllers (06/16/2011)
/timelog_controller.rb
/projects_controller.rb
/news_controller.rb
/issues_controller.rb
/users_controller.rb
Builder Templates
./app/views/issues/index.api.rsb
./app/helpers/custom_fields_helper.rb:109: def render_api_custom_values(custom_values, api)
Jquery UI Dialog and multiple Dialogs on one page / Jquery UI Dialog only opens once
When you work with the Jquery UI Dialog Widet, you might come across an issue with the dialog only showing once. This happens because once you initialize the dom element the element that you use for the dialog gets removed from the dome, and stored into the Dialog instance. One of the core Jquery UI developers describes an easy solution for this issue in his blog. But the described way to solve the issue still needs to be patched, when you have multiple dialogs on the page, because from the link that you use to open the dialog, is no easy way to determine which of the already initialized dialogs to open.
What i did below is to give each dialog link an unique id (in this case “dialog_#{someactiverecordobject.id}”, and use it to ‘name’ the dialogs and therefore be able to know which one to open.
$(function() {
var dialogs = {}
$('.dialog').each(function() {
var dialog_id = $(this).prev('.dialog_link').attr('id');
dialogs[dialog_id] = $(this).dialog({autoOpen: false, title: '<b>Details</b>'});
});
$('.event_details_link').click(function() {
dialogs[$(this).attr('id')].dialog('open');
return false;
});
});Rails 3 Lernen mit d:evolute
Aus den Archiven unserer Software - Abteilung d:evolute ein Einsteiger - Guide fuer Rails (3)
Compound Index in Rals and a lot more SQL explanation
Doing a research on compound indices in Rails3, i found this article with a loot of cool information, benchmarks end detailled explanation of different queries (using indices)
Friendly No-SQL - like ORM for MySQL, Serialization and Custom Fields
We at devolute and brueckenschlaeger work on technologies, that allow you to have your SQL - Databases administered as schemaless as possible.
Consider the typical case of a Feedback form or a simple Survey.
You just don't wanna create a new table for every type of questionnaire. Plus performance (on read operation) is not the main problem here., you want to focus on fast write and later export your data and analyze it somewhat.
There are two basic technologies, I know to do that:
- Serialization
- A Thing called 'Custom Fields'
Serialization let's you simply dump your schema into a text column. Try it, it does not hurt. With rails it's dead simple anyway.
Drawbacks and pitfalls exist :)
Custom Fields - well, take a look at redmine. The 'Issues' are like a schemaless model with a bit basic schema (one could leave that). And now you have fields and field-object relation.
So the basic schema less database in Rails would look like this:
<pre>
class Thing < ActiveRecord::Base
has_many :properties, :through => :relations
end
class Relation < ActiveRecord::Base
belongs_to :thing
belongs_to :property
end
class Property < ActiveRecord:;Base
has_many :things, :through => :relations
end
</pre>
As you see, now every property could exist in several things. You might not want that, but Rails doesn't have has_one .. :through. Work around it.
In Property you would have two columns:
- name: string
- data: text
I think you get the idea.
Now, to the topic - just found this: http://www.rubyinside.com/friendly-easy-schemaless-nosql-data-storage-with-mysql-in-ruby-2908.html
OneAim Content Management Released
Finally I released the Content Management used on OneAim.
OneAim is the following:
a) There is one aim
b) Projects having that one aim can be open or finished.
Great, what?
As the time goes by and the project becomes more and more old (Rails 2) I release it.
How to set up Compass on Rails 1
Because no one in my team ever wrote it down, i will do that now for you:
It's pretty easy:
compass create myproject
mate myproject
compass watch myproject
compass init rails /path/to/myrailsproject
with blueprint
compass init rails /path/to/myrailsproject --using blueprint/semantic