vhochstein / active_scaffold

Rails 4 Version of activescaffold supporting jquery
MIT License
156 stars 34 forks source link

Multi layer belongs_to #58

Closed danil-z closed 13 years ago

danil-z commented 13 years ago

Hi folks i have 3 simple models class Region < ActiveRecord::Base has_many :cities has_many :schools, :through => :cities end

class City < ActiveRecord::Base
  belongs_to :region
  has_many :schools
end

class School < ActiveRecord::Base
  belongs_to :city
end
  1. How to define SchoolsController activescaffold config to add region column?
  2. at SchoolsController is it possible have all columns (region and city) add an school and choose region first and scoped city (after changed region)
vhochstein commented 13 years ago

I would say that this question is a top candiate for activescaffold group, but anyway a few hints to get you to your goal:

  1. take a look at activerecord delegates feature, http://www.rorexperts.com/delegate-attribute-in-activerecord-t1284.html, https://github.com/activescaffold/active_scaffold/wiki/Search-on-second-order-association (example is for search form you have to adapt)
  2. Place region select box before city select box in create form and take a look at update_columns feature of activescaffold: https://github.com/activescaffold/active_scaffold/wiki/Chaining-Form-Fields
danil-z commented 13 years ago

in school model i put following code delegate :region, :to => :city , :allow_nil => true

and in controller active_scaffold :school do |conf| conf.columns = [:title, :region, :city ] end i get column like this in list view

Region:0xb3d39c4

i tried add to region model

class Region < ActiveRecord::Base
 has_many :cities,  :dependent => :destroy
 has_many :schools, :through => :cities
 def to_label
  "#{name}"
 end
end

but it has no affect to view in rails console if run School.find(:first).region.class i get => Region(id: integer, name: string, created_at: datetime, updated_at: datetime)

danil-z commented 13 years ago

i tried tons of combinations but have no success on them...

Started GET "/schools" for 127.0.0.1 at 2010-12-29 12:11:16 +0300 ActiveScaffold: Missing date picker localization for your locale: ru ActiveScaffold: Missing datetime picker localization for your locale: ru Processing by SchoolsController#index as HTML SQL (0.3ms) SELECT COUNT() FROM "schools" School Load (0.3ms) SELECT "schools". FROM "schools" ORDER BY "schools"."id" ASC LIMIT 50 OFFSET 0 City Load (0.3ms) SELECT "cities".* FROM "cities" WHERE ("cities"."id" = 1) Region Load (0.3ms) SELECT "regions".* FROM "regions" WHERE ("regions"."id" = 1) Rendered vendor/plugins/active_scaffold/frontends/default/views/_action_group.html.erb (5.2ms) Rendered vendor/plugins/active_scaffold/frontends/default/views/_list_header.html.erb (17.7ms) Rendered vendor/plugins/active_scaffold/frontends/default/views/_list_column_headings.html.erb (5.4ms) Rendered vendor/plugins/active_scaffold/frontends/default/views/_messages.html.erb (1.4ms) Rendered vendor/plugins/active_scaffold/frontends/default/views/_list_messages.html.erb (14.6ms) Rendered vendor/plugins/active_scaffold/frontends/default/views/_list_record_columns.html.erb (13.4ms) Rendered vendor/plugins/active_scaffold/frontends/default/views/_action_group.html.erb (77.1ms) Rendered vendor/plugins/active_scaffold/frontends/default/views/_list_actions.html.erb (90.1ms) Rendered vendor/plugins/active_scaffold/frontends/default/views/_list_record.html.erb (126.3ms) Rendered vendor/plugins/active_scaffold/frontends/default/views/_list_pagination.html.erb (2.0ms) Rendered vendor/plugins/active_scaffold/frontends/default/views/_list.html.erb (192.4ms) Rendered vendor/plugins/active_scaffold/frontends/default/views/_list_with_header.html.erb (231.8ms)

vhochstein commented 13 years ago

Well :region is a virtual column, which means that you have to configure it on your own in your active_scaffold_config. eg if you would like to call to_label on region you have to generate a form column override which actually does that.

danil-z commented 13 years ago

to test helper override i've added

module SchoolsHelper
 def school_region_column(record)
  "1111"
 end
end

but it doesn't show 1111 in region column...(regarding API it should be affected in list and show actions)..... why? sorry this stupid questions but i really stuck with them for several days and it drives me crazy...

danil-z commented 13 years ago

ok finnaly i find way why it didin't work

def region_column(record)
 "1111"
end

it should be named like this main question: which version of api should i review on original activescaffold wiki seems In v2.3 and previous versions format was #{column_name}_column, so method was named username_column

prior 2.3 ?

vhochstein commented 13 years ago

Yes, without the controller name at the beginning

danil-z commented 13 years ago

steal can't handle 2nd question

  1. Place region select box before city select box in create form and take a look at update_columns feature of activescaffold: https://github.com/activescaffold/active_scaffold/wiki/Chaining-Form-Fields i made module SchoolsHelper def region_column(record) record.region.name end def region_form_column(record,options) select(:record, :region, Region.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'}) end def options_for_association_conditions(association) if (association.name == :city) && @record.region {'cities.region_id' => @record.region} else super end end end and now allcities shos but if i change region nothings happen... it should do xhr.request to server? or options for server should be preloaded? i've selected $("#record_region") in javascript console and no binded events on change or other events.... how should it work?
danil-z commented 13 years ago
active_scaffold :school do |conf|
  conf.columns = [:title, :region, :city ]
  conf.columns[:region].form_ui = :select
  conf.columns[:region].update_columns = :city
  conf.columns[:city].form_ui = :select
end

i have this in controller atm

vhochstein commented 13 years ago

Change def region_form_column(record,options) select(:record, :region, Region.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'}) end

to def region_form_column(record,options) select(:record, :region, Region.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'}, options) end

danil-z commented 13 years ago

i got error Started GET "/schools/render_field?column=region&update_columns[]=city&value=2" for 127.0.0.1 at 2010-12-29 16:26:25 +0300 Processing by SchoolsController#renderfield as /_ Parameters: {"column"=>"region", "update_columns"=>["city"], "value"=>"2"} Completed in 7ms

NoMethodError (undefined method `region=' for #<School:0xa1ccba4>):

in model i have delegate :region, :to => :city , :allow_nil => true i'm not sure what i should do ...

danil-z commented 13 years ago

i have a copy on github of my application git://github.com/blackbumer/dnevnik.git maybe i have something wrong in other place than model?

vhochstein commented 13 years ago

Ok, it s getting interesting.

delegate method is just creating a getter method... now you need a setter method, cause current region value has to be stored. so you have to create a setter in your model.

However, you are running now in some trouble, cause delegate is nt working for you anymore. So i think you have to do it on your own and write your own getter.

Maybe something like the following def region @region ||= self.city.try(:region) end

def region=(region) if region.is_a? Region @region = region else @region = Regions.find(region) end end

danil-z commented 13 years ago

omg it's finally working! thank you a lot now i need clean my mind and review all code may be i should write some wiki page about such situation? to help others (by simple example )

vhochstein commented 13 years ago

Great, that would be a good idea.

BTW: If just updated the ru locale file to at least include all new keys. Would be great if you could take a look and add corresponding translation. Thanks a lot.

danil-z commented 13 years ago

sure i could add it but how to update your plugin correctly? i've installed as described at your blog howto rails plugin install git://github.com/vhochstein/active_scaffold.git if i just clone it with git command it will rewrite all files? (will reset jquery settings inside enviroment files in plugin folder...) sory for stupid question again i just started with ruby and rails 3 weeks ago... and not familar with git much too :)

danil-z commented 13 years ago

1 more little question :

def region_form_column(record,options)
  select(record, :region, Region.all.collect {|p| [ p.title, p.id ] }, {:include_blank => t('choose_region')}, options)
end

how to make this helper point selected element (if i update a school witch already have selected city,it should show select input with slected region of city...)

danil-z commented 13 years ago

btw your generator active_scaffold_setup for jquery inserts jquery-ui.js and stylesheets but seems my pages don't use it for the scaffold component... may be need to remove frome generator? just a sugesstion...

vhochstein commented 13 years ago

jquery ui is needed for example for datepicker,... so I do not recommend deleting it.

@select_issue: take a look at def active_scaffold_input_enum in form_column_helpers

@russian locale: best but not easiest way: fork my repository make changes to russian local and send me a pull request. easiest way: change russian locale and upload the file to somewhere I can reach it.

danil-z commented 13 years ago

thanks for your help all is clear for me now :)

vhochstein commented 13 years ago

You are welcome