This project is archived and is in readonly mode.
Foreign key is not set on nested record
-
Alexander Zubkov
Don't you agree it is a bug? Any comments?
-
Rohit Arondekar
- Importance changed from to Low
Alexander, could you please paste the relevant code of the models as well for completeness. Also are you using
@photo.photo_albums.build(params[:photo_album])— since#create&#newwill not fill thephoto_album_idautomatically while#buildwill. -
Alexander Zubkov
class PhotoAlbum < ActiveRecord::Base
attr_accessible :name, :description, :visiblehas_many :photos, :dependent => :destroy
validates_presence_of :name
scope :visible, lambda { |admin| where(:visible => true) unless admin }
cattr_reader :per_page @@per_page = 6 end
class Photo < ActiveRecord::Base
attr_accessible :name, :position, :photo, :visible, :photo_album_idbelongs_to :photo_album
has_attached_file :photo, :styles => {
:cover => '100x100#', :thumb => '150x150#', :original => '980x720>' }before_post_process { translit_paperclip_file_name self.photo }
scope :visible, lambda { |admin| where(:visible => true) unless admin }
before_save :set_position, :on => :create
protected def set_position
self.position = 1 + Photo.where(:photo_album_id => self.photo_album_id).maximum(:position).to_iend end
Well, I use cancan with load_resource, so I skip that line:
@photo.photo_albums.build(params[:photo_album]) in my controller. I don't know, if cancan is using #new or #build, but probably it is a cancan bug, I will check and answer later. Thank you. -
Rohit Arondekar
I have not used cancan before but just browsing it's source code on Github I found https://github.com/ryanb/cancan/blob/master/lib/cancan/controller_a...:
# [:+new+] # Specify which actions are new resource actions in addition to :+new+ and :+create+. # Pass an action name into here if you would like to build a new resource instead of # fetch one. # # load_resource :new => :buildAre you using that? If not it might be what you are looking for.
-
Alexander Zubkov
Yes, you're right. #new does not set foreign key, even on Rails 2.3. So probably cancan behavior has changed, so I had to add that line. Thank you for explanations. It's not Rails bug, let's close this ticket.