Polymorphic Associations
class Address < ActiveRecord::Base belongs_to :addressable, :polymorphic => true
# sql cols: addressable_id, addressable_type
end
class User < ActiveRecord::Base
has_one :address, :as => :addressable
end
User.find(:first).address
Shipment.find(:first).address
with_scopehttp://scottraymond.net/articles/2006/02/28/rails-1.1
Member.count # => SELECT COUNT(*) FROM members
Member.find(1) # => SELECT * FROM members WHERE id = 1
Member.delete_all # => DELETE FROM members
Member.with_scope(:find=>{:conditions=>"group_name = 'Berryz'"},
:create=>{:group_name => 'Berryz'}) do
Member.count # => ... WHERE group_name = 'Berryz'
Member.find(1) # => ... WHERE group_name = 'Berryz' AND id = 1
Member.delete_all # => ... WHERE group_name = 'Berryz'
Member.create
# >> #<Member: @attributes=>{:group_name=>"Berryz"}>
end
more at
More at,
http://scottraymond.net/articles/2006/02/28/rails-1.1
rails mywebapp
cd mywebapp
ruby script/server
(windows user type script\server instead)
<form action="/student/save_new" method="post">If you submit, you can retrieve the parameters in your controller via params[:user_id], params[:user_password]
<input type="textfield" name="user_id" />
<input type="textfield" name="user_password" />
<input type="submit" />
</form>
class StudentController < ApplicationControllerBut did you know that, is the form fields becomes...
def save_new
User.create(params) # params is a Hash
end
end
<form action="/student/save_new" method="post">If you submit, you retrieve the parameters with params[:user][:id], params[:user][:password]
<input type="textfield" name="user[id]" />
<input type="textfield" name="user[password]" />
<input type="submit" />
</form>
def save_newAs a bonus,
User.create(params[:user]) # params[:user] is a Hash with :id and :password as keys
end
params[:hobby] == ["basketball", "baseball", "soccer"] # if all checkboxes were selectedSo, extrapolate a bit..
params[:user][:hobby] == ["basketball", "baseball", "soccer"] # if all checkboxes were selectedWhat's all these [] magic good for? Well, if you have a form for creating only 1 user, any of the HTML forms above is fine. Really. However, if your form need to allow user to enter say,.. 10 records at the same time (i.e. 10 pairs of names and ids... )... in the Rail-ish way, you can now do:
<input type="textfield" name="user[0][id]" />
<input type="textfield" name="user[0][password]" />
<input type="textfield" name="user[1][id]" />
<input type="textfield" name="user[1][password]" />
<input type="textfield" name="user[2][id]" />
<input type="textfield" name="user[2][password]" />
... blah blah blah, of course you can do a for loop inside the rhtml instead of hand coding..
You can parse the form values in your controller action like this:
10.times do |x| # loops thru 0 to 9Now, doesn't that look much more elegant and succinct than what other platforms allow you to do?
User.create(params[:user][x]) # params[:user][x] is a Hash of :id and :password
end
render :partial => "template_name", :collection => ["Alpha", "Beta", "Charlie"]I can then reference the objects in _template_name.rhtml with automagically created local variables:
<%= template_name %>, index <%= template_name_counter %><br />The above should produce the following HTML:
Alpha, index 0<br />
Beta, index 1<br />
Charlie, index 2<br />