ruby on rails - Derive ActiveRecord model field from id -
I am writing an application to parse my iTunes xml library in a database in Ruby and I'm having a problem .. I have a wrapper model in which there is a field, file name, which I need to get from another field, album_id in that field
file name = Album_ID 10 '0 and jpg extension Is frustrated by ...
but field ALBUM_ID, as the name suggests Security is a foreign key to another model, album. I only get ALBUM_ID while saving the album ...
Is there a way to get my file name folder to get ALBUM_ID using callback, I do not know, maybe BEFORE_SAVE or AFTER_SAVE?
- == UPDATE == -
Make me clear ... I have a field in the table name FILENAME ... this field The file name of the cover is in this format
ID 1 = 0000000001.jpg for album with id = 5600000056.jpg
Cover the album model There is a HAS_MANY relationship with the model ... When I save the album, I have to get the saved value in ALBUM_ID in the cover model and create a string in that format and Must use the file name field.
I am doing, in the second part, to copy jpgs to cover the broom in another library folder in another library ... This is why I need to rename the files
As I have understood that you have a cover
model By using related_to
the album
model, album_id
field.
When creating a Now that you After reading your post again, the only purpose of your If you have jpg show.html.erb album
, you can easily .build
album = album.new (: name = & "Foo",: Artist => "Bar", ...) album.cover = Cover.build (: filename => "filename.png") # Save when you are saving the album
album
is automatically generated from cover
with correct album_id
. Second Solution
cover
model is to generate a JPG with the album ID. 00000000001 .jgp
, why would not you name it in a helper or idea?
& lt; Img url = "0000000000 & lt;% = @ album.id%> .jpg" / & gt;
Comments
Post a Comment