Pontek
Pontek

Reputation: 149

create a model in create action from a class

As a newbie to rails I can't find how to solve my issue ^^
I want to create a VideoPost from a form with a text field containing a video url (like youtube)
I'm getting information on the video thanks to the gem https://github.com/thibaudgg/video_info
And I want to save thoses information using a model of mine (VideoInformation). But I don't know how the create process should work.
Thanks for any help !

I'm trying to create a VideoPost in VideoPostsController like this :

def create
  video_info = VideoInfo.new(params[:video_url])
  video_information = VideoInformation.create(video_info)      #undefined method `stringify_keys' for #<Youtube:0x00000006a24120>
  if video_information.save
    @video_post = current_user.video_posts.build(video_information) 
  end
end

My VideoPost model :

# Table name: video_posts
#
#  id                   :integer          not null, primary key
#  user_id              :integer
#  video_information_id :integer
#  created_at           :datetime         not null
#  updated_at           :datetime         not null

My VideoInformation model (which got same attributes name than VideoInfo gem) :

# Table name: video_informations
#
#  id              :integer          not null, primary key
#  title           :string(255)
#  description     :text
#  keywords        :text
#  duration        :integer
#  video_url       :string(255)
#  thumbnail_small :string(255)
#  thumbnail_large :string(255)
#  created_at      :datetime         not null
#  updated_at      :datetime         not null

Upvotes: 0

Views: 218

Answers (2)

Jason Noble
Jason Noble

Reputation: 3766

I would add a method to the VideoInformation model so you can create one by passing in video_info:

# app/models/video_information.rb
def self.create_from_video_info(video_info, url)
  video_information = self.new
  video_information.title = video_info.title
  video_information.description = video_info.description
  video_information.keywords = video_info.keywords
  video_information.duration = video_info.duration
  # video_url appears to not be available on video_info,
  # maybe you meant embed_url?
  video_information.video_url = url
  video_information.thumbnail_small = video_info.thumbnail_small
  video_information.thumbnail_large = video_info.thumbnail_large
  video_information.save
  video_information
end

# app/controllers/video_posts_controller.rb
def create
  video_info = VideoInfo.new(params[:video_url])
  video_information = VideoInformation.create_from_video_info(video_info, params[:video_url])

  if video_information.valid?
    current_user.video_posts << video_information
  end
end

Also, you may want to look at doing this a different way. It seems redundant to have VideoInformation, VideoInfo and VideoPost classes.

Maybe the VideoPost model could simply store the URL of the video, and you could pull in the VideoInfo stuff on the fly as you need it when rendering/using VideoPost instances.

Upvotes: 2

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230306

don't know how the create process should work

create method expects a hash with parameters, not some arbitrary object. You should use methods of VideoInfo and convert it to a hash which will be consumable by ActiveRecord.

Upvotes: 3

Related Questions