-
Notifications
You must be signed in to change notification settings - Fork 7
HowTo : Columns for associations
giniedp edited this page Jul 4, 2012
·
3 revisions
Nested models may be added like this and do work for belongs_to associations
def index
fancygrid_for :users do |user|
# ...
# insert associated models columns
user.columns_for :contact do |contact|
contact.attributes :email
# multiple nesting hierarchies are also supported
contact.columns_for :person do |person|
person.attributes :first_name, :last_name
end
end
user.find do |query|
# the associated models always should be included in the search for eager loading.
query.includes :contact => :person
end
end
end
If you have Users that has_many Orders, you should rather define a fancygrid for the Orders than for Users. However, if it must be a Users table and you want to search on the associations attributes, you can do that:
def index
fancygrid_for :users do |g|
# ...
g.columns_for :roles do |roles|
roles.attributes :name
end
# ...
end
end
The definition is valid, and you can already search for users with a specific role. But nothing is going to be rendered in the roles.name column. This is because roles is a collection of records, and not a single record. You can now format the column in the view like this
= fancygrid :users do |column, record, value|
- case column.identifier
- when "roles.name"
= record.roles.map(&:name).join("|")
- else
= value