1

I am developing RoR application that works with legacy database and uses ActiveScaffold plugin for fancy CRUD interface.

However one of the tables of my legacy db has composite primary key. I tried using Composite Keys plugin to handle it, but it seems to have conflicts with ACtiveScaffold: I get the following error:

ActionView::TemplateError (Could not find column contact,type) on line #3 of ven
dor/plugins/active_scaffold/frontends/default/views/_form.rhtml:
1: <ol class="form" <%= 'style="display: none;"' if columns.collapsed -%>>
2:   <% columns.each :for => @record do |column| -%>
3:   <% if is_subsection? column -%>
4:   <li class="sub-section">
5:     <h5><%= column.label %> (<%= link_to_visibility_toggle(:default_visible =
> !column.collapsed) -%>)</h5>
6:     <%= render :partial => 'form', :locals => { :columns => column } %>

vendor/plugins/active_scaffold/lib/data_structures/sorting.rb:16:in `add'

while having in model code smth like:

set_primary_keys :contact, :type

I highly appreciate any idea how I can get composite keys capability with ActiveScaffold.

3 Answers 3

2

I think your best bet may be checking the ActiveScaffold Google Group as it's monitored by core developers of ActiveScaffold and they would ultimately be able to solve your problem and explain why composite keys with the plugin won't work with ActiveScaffold.

Good luck and be sure to post a follow-up if you do get results from the Google Group (which I have posted on before and received feedback very quickly).

One quick result I did find was this.

What I did was to create a facade class that does not inherit from
ActiveRecord then make the "id" show the primary key. In my case the
primary key was computed from other data and could change as a result
of an edit, so I had to override ActiveScaffold in a few places to
allow for the primary key changing after an update. But, all in all
it works and is fairly straightforward. Start with an empty class
and just resolve messages that are not understood. In your case you
could even just redirect all messages to a wrapped ActiveRecord while
replacing the id and id= methods, and filtering the [] and []= methods.

That may do the trick for you.

0

No, I have not received any reply from the group and I am not sure if ActiveScaffold is actively maintained yet.

After some time playing with ActiveScaffold, I ended up implementing my own CRUD interface from the scratch.

0

I have this working, with read-only models, using ActiveScaffold on a legacy DB.

The trick was to override the default 'id' field in the model and return a concatenated PK string.

If that's good enough, then here you go:

  class CPKReadonlyModel < ActiveRecord::Base
    set_primary_key :id_one # only half of it, but id overridden below...

    def id
      self.id_one.to_s + ',' + self.id_two.to_s
    end

    def readonly?
      true
    end

    def before_destroy
      raise ActiveRecord::ReadOnlyRecord
    end

    def delete
      raise ActiveRecord::ReadOnlyRecord
    end

    def self.delete_all
      raise ActiveRecord::ReadOnlyRecord
    end
  end

The controller has the following in the active_scaffold config block:

  config.actions.exclude :create, :update, :delete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.