Sunday 15 April 2012

ruby on rails - How to create permalink with id? -


How do I create a permacetime with an ID for a new model?

Example

  Animal = Animal. Creating (name: 'cool dog') surgery does animal with the name of the # 1 and the dog's animal name. Urinal = & gt; "1-Cold-dog"   

How do you add the proper callback so that the ID is inserted? before_save or after_save does not work

  after_save: update_permalink #or before_save def update_permalink self.permalink = "# {id} # {name} } ". Paramerize and   

What's happening? Instead of "1-cold dog" I get "Cool-dog"

And why do I find it on after_save Setting up an attribute without saving but does not work on first_save because the ID is not built on a new record.

According to

you should use after_save

Save and destroy both, wrapped in a transaction, it ensures that you will be under the cover of what you do in the verification or callback. You can use assumptions to check the values ​​that depend on the transaction or you can increase the exception of rollback in the callback, which includes the after_ * callback.

As a result of the database changes are not seen outside your connection until the operation is complete. For example, if you later attempt to update the indicator of a search engine, then not after viewing the updated record for the index. There is a one-time callback update that is updated once. see below.

As I have mentioned above, you may like it compared to your animal model.

  def to_param "# {id}} - # {name.parameterize}" end   

This will automatically all your URLs like that permelink Which you are trying to create and you still have Animal.find (params [: id])

No comments:

Post a Comment