Archive for July, 2007

ActiveScaffold: displaying different columns for a model when it is to be a subform

Problem: I wanted to display all columns when doing an update or create of a model but when that model was to be updated or created in a subform of an associated model’s create or update I only wanted certain columns (the required ones) to be displayed.

Solution: config.subform.columns.exclude :field_name

So, if a user is to be edited and the user groups appear as a subform in the scaffold, the snippet above must be in the code for the user group, NOT the user. If you don’t want to include the group category and group note in the user update scaffold, the line of code in the groups_controller would be config.subform.exclude :category, :note

Comments (2)

Image depicting Ruby on Rails data model relationships

This image is useful in illustrating data model relationships in Ruby on Rails.

http://mboffin.com/stuff/ruby-on-rails-data-relationships.png

Comments

ActiveScaffold and Rails: conditions_for_collection

Use this method in your controller to restrict the items to be displayed in your list to those that meet the conditions set in the method. The example from the ActiveScaffold wiki is as follows:

def conditions_for_collection
['user_type IN (?)', ['admin', 'sysop']]
end

Comments

Implicit Mapping Tables in Ruby on Rails

When creating an implicit mapping table (habtm table) to join two otherwise unrelated tables (e.g. groups and projects) that have a habtm relationship and have primary keys, make sure that the mapping table does not have an id column but adding the ‘, :id => false’ before the ‘do’ in your migration setup. If this is not done there will be errors related to the primary key when you attempt to add new relationships.

From the rubyonrails.org docs: “When you create your habtm table, make sure to leave out the “id” column. Otherwise, the habtm join table id will be populated to contain the id of your joined model, not an auto-inc number. this, of course, leads to trouble.”

Comments