开发者

friendly_id and ActiveScaffold conflict

开发者 https://www.devze.com 2023-02-05 14:17 出处:网络
I have friendly_id and ActiveScaffold installed for my Rails application. Because not all of my models have unique name fields I have to开发者_StackOverflow社区 use the Slugged Model to make it work.

I have friendly_id and ActiveScaffold installed for my Rails application.

Because not all of my models have unique name fields I have to开发者_StackOverflow社区 use the Slugged Model to make it work. friendly_id does the job flawlessly I have friendly URLs and I can load the objects using the friendly id.

But when I want to create a new object with ActiveScaffold, it says the following error message:

ActiveScaffold::ReverseAssociationRequired (Association slugs: In order to support :has_one and :has_many where the parent record is new and the child record(s) validate the presence of the parent, ActiveScaffold requires the reverse association (the belongs_to).)

Of course I cannot create the belongs_to association in that side because it's created by the friendly_id module and every model which works slugged way should be included there.

The model looks like this:

class FooBar < ActiveRecord::Base
  has_friendly_id :name, :use_slug => true, :approximate_ascii => true
end

In my ApplicationController:

class Admin::FooBarsController < Admin::ApplicationController
  active_scaffold :foo_bar do |config|
    config.list.columns = [ :id, :name ])
    config.update.columns = [ :name ]
    config.create.columns = config.update.columns
  end
end

Is there a way to make this work?

The versions: friendly_id 3.2.0, ActiveScaffold latest in the rails-2.3 git branch.

UPDATE: Seems like it does not conflict in production mode.


calling

has_friendly_id :name, :cache_column => 'cached_slug', :use_slug => true

... creates a has_many and a has one associations pointing to a slug AR model which hasn't any polymorphic belongs to association properly defined.

So basically what you need to do to solve this error is to define the reverse associations in the controller of your parent model (the one who has friendly_id stuff)

  active_scaffold :products do |config|
    ...
    config.columns[:slug].association.reverse = :product
    config.columns[:slugs].association.reverse = :product
  end

and it works :-)

PS : I use friendly_id as gem and ActiveScaffold VHO master branch for rails 3


In the past I have the same problem , i have solved , but i dont remember my solution , lookin at my code the only relevant hack is to use friendly_id as plugin and load it at last with config.plugin in environemnt.rb

aviable_plugins = Dir.glob(RAILS_ROOT+"/vendor/plugins/*").collect {|i| i.split("/").last }
config.plugins  = aviable_plugins + [:friendly_id] #friendly_id must be last

I'M NOT SURE ,sorry, but if you try let my know.

sorry for my english to

0

精彩评论

暂无评论...
验证码 换一张
取 消