I have the following classes:
class VideoChannel < ActiveRecord::Base
#Associations
belongs_to :video_playlist, :dependent => :destroy
VideoChannel.video_playlist_name
delegate :name, :id, :list_type, :list_id, :manual, :to => :video_playlist, :prefix => true
#validations
validates_presence_of :name
#After Functions
def after_create
video_playlist = VideoPlaylist.new(:name => self.name,
:list_type => "VideoChannel",
:list_id => self.id)
video_playlist.save
end
And :
class VideoPlaylist < ActiveRecord::Base
belongs_to :list, :polymorphic => true
has_many :video_channels, :dependent => :destroy
delegate :name, :id, :description, :to => :video_channel, :prefix => true
end
I’m trying to use the Rails Delegate function to create a link in the VideoChannel page that allows me to to link to the Video Playlist and edit the contents there. So the association is there and You can currently edit the playlists by going through the playlists section but we want to combine them. I can’t seem to figure this out. Im also very new to Rails, still working through the guides etc.
Edit: Here’s the view code
<%= link_to '<span class="pen icon"></span>Edit',
content_url(:controller =>"video_playlists", :id => channel.video_playlist_id, :action => "edit"),
:class => "button right" %>
Here are teh relevant pieces of the controllers:
class VideoChannelsController < ApplicationController
# GET /videochannels
# GET /videochannels.xml
def index
@video_channels = VideoChannel.roots(:order => 'order_num')
@video_channels_parents = @video_channels.group_by {:parent_id}
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @video_channels }
end
end
# GET /videochannels/1
# GET /videochannels/1.xml
def show
@video_channel = VideoChannel.find(params[:id], :order => 'order_num')
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @video_channel }
end
end
end
class VideoPlaylistsController < ApplicationController
# GET /video_playlists
# GET /video_playlists.xml
def index
if !params[:with].nil?
@video_playlists = VideoPlaylist.find(:all, :conditions => {:list_type => 'VideoShow'})
else
@video_playlists = VideoPlaylist.find(:all, :conditions => {:list_type => 'Section'})
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @video_playlists }
end
end
# GET /video_playlists/1
# GET /video_playlists/1.xml
def show
@video_playlist = VideoPlaylist.find(params[:id], :include => [{:video_video_playlists => :video}, {:videos => :asset}, {:videos => :content_image}])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @video_playlist }
end
end
end
Where did the line
Come from? What’s it doing? You’re also calling a method on the class not an instance (sort of – Ruby isn’t quite like this, but it’s enough to explain).
Anyway:
Delegate is really for avoiding lots of train wreck code like this:
You’ve said that they belong to each other – the relationships look like they’re the wrong way round. Why are you creating the parent from inside the child? How are you going to have many video channels belonging to a given playlist?
I think you need to look at build and the relationship names. To gat past me maybe misunderstanding your model, lets have switch to a product that has many stock items:
This means that stock_item will have a column product_id.
So, say you’re creating a product you’d do something like:
This automatically sets the id’s for you and means you don’t have to set id’s. Then when you do product.save it will save all the related stock items too.
And in the view for this toy model, then if you were displaying one of the stock items, you’d use delegate to show the name of the product in the view without having to do lost of stock_item.product.name (for example).
I hope this helps.