Wesly Mezarina

Joined

620 Experience
1 Lesson Completed
0 Questions Solved

Activity

Posted in Updating a genre in a nested association

I figured out how to update it to each anime. I'll show my code below.

genre_scrape = doc.css('div#content .borderClass .js-scrollfix-bottom div:contains("Genres")').text.split(' ')[1..-1]
genre_text   = genre_scrape.blank? ? "" : genre_scrape
genre = []

genre_text.each do |g|
  genres = g.gsub(/\,/,"")
  genre << Genre.where(title: genres).first_or_create do |genre|
    genre
  end
end

anime.update(genres: genre)

Posted in Updating a genre in a nested association

So i figured out how to check each genre in the array with the code below. Now i need to try to figure out how to update the genre to each anime. If i try to put anime.update(genres: genre) where i have the #code i get an error undefined method each for #<Genre:0x007fecaa98a9d8>. I'll keep updating.

genre_text.each do |g|
 genres = g.gsub(/\,/,"")
  Genre.where(title: genres).first_or_create do |genre|
   #code
  end
end

Posted in Updating a genre in a nested association

I've been asking a few question on scraping and nested association. Im running into another issue which is updating genres to each anime.
I'm getting a list of genres, for example "Action, Romance, Drama" and I'm separating each one as so genre_text.strip.split(' ').
I'll just show all my code for a better understanding below. I keep getting an error undefined methodeach' for #`. I tried putting it in a do block but i just updates it all in an array. I'll show how i had it in a do block as well.
Is there a way that I can update each one with a separate genre to each anime?

rake file

genre_scrape = doc.css('div#content .borderClass .js-scrollfix-bottom div:contains("Genres")').text.split(' ')[1..-1]
genre_text = genre_scrape.blank? ? "" : genre_scrape.join(' ').strip.split(' ')
genre = Genre.find_or_create_by(title: genre_text)
anime.update(genres: genre )

With do block

genre_scrape = doc.css('div#content .borderClass .js-scrollfix-bottom div:contains("Genres")').text.split(' ')[1..-1]
genre_text = genre_scrape.blank? ? "" : genre_scrape.join(' ').strip
genres = []
genre_single = []
genre_text.strip.split(' ').each do |g|
 genre_single << g.gsub(/\,/,"")
end
Genre.find_or_create_by(title: genre_single) do |genre|
 genres << genre
end
anime.update(genres: genres )

Genre model

class Genre < ActiveRecord::Base
 has_and_belongs_to_many :animes
end

Anime model

class Anime < ActiveRecord::Base
 has_and_belongs_to_many :genres
end

Do you guys know of another possible solution? I tried Drazen's code but i kept getting nil class error. With the code i provided it just keeps making a new season and only assigned one anime to the season.

So i figured out that i can use anime.update(:season_attributes => {:title => season}) to update it but i get a weird url.
I get something like "spring-2016-fba9c08e-458e-4b50-93e4-ca41a522ed67". It might be because i already have Spring 2016 in the DB.
I'll keep updating.

So pretty much a anime belongs_to :season and a season has_many :animes.
I'm scraping a site and i'm collecting the season like "Spring 2015". I want to be able to assign "Spring 2015" to a anime so it can go to the collection of "Spring 2015". Is there a way to doing this? I was thinking of doing anime.update_attribute(:season_id, season) but that wont work.

My .rake file

desc "Fetch anime info"
task :fetch_anime_info => :environment do
 require "open-uri"
 require 'mechanize'

 Anime.all.each do |anime|
  season_scrape = doc.css('div#content .borderClass .js-scrollfix-bottom div:contains("Premiered")').text.split(' ')[1..-1]
  if season_scrape.blank?
   season = ""
  else
   season = season_scrape.join(' ')
  end

  anime.update_attribute(:season,  season)
 end
end

Anime model

class Anime < ActiveRecord::Base
 belongs_to :season
end

Season model

class Season < ActiveRecord::Base
 has_many :animes
end

Thank you so much. This worked perfect.

That worked perfect, but now i ran into another issue. Rather then only being question marks some are displayed as "??? ??, 2001" or "03 ??, ????.
I have to look for a work around.

For line.text it looks for the date and on some that don't have any date's it will get the question marks because that's what the site has to replace the date.

How would you go to setting an if statement to check? I have not been having any luck.

I'm scraping a anime information website. Right now, I'm trying to get the date going, but on some of the anime the date comes out as ???,??,????. I'm tying to figure out how to skip that or put some text like "No date" or just put 00,00,0000.

This is what i have so far and it works until it sees that "???????" can not be turned into a integer.

page.css('span.remain-time').each do |line|
 n = 3
 d = line.text[/(\S+\s+){#{n}}/].strip
 date = Date.parse(d)
 puts date
end

I'm able to show a stream of activity for each user in their profile. What I'm trying to figure out is how i can have a book show one time with the activity that a specific user provided on the side like Favorite, Liked and so on rather then showing the same book multiple times with the activity next to it. How do i show a specific book and show all the activity that a uniq user provided?

This is my UsersController. I use the @activities to show the users activity.

 class UsersController < ApplicationController
  def show
   @user = User.friendly.find(params[:id])
   @activities = PublicActivity::Activity.order("created_at desc")
      .where(owner_id: @user.id, owner_type: "User")
  end
 end

This is my _activity.html.erb page

 <% @activities.each do |activity| %>
  <%= link_to image_tag(activity.trackable.cover_image.url{:large}), activity.trackable.name%>
  <%= render_activity activity %>
 <% end %>

Posted in How do I make comments show the user who created it?

I'm not sure how i missed that simple mistake. Thanks!

Posted in How do I make comments show the user who created it?

I get this error

 undefined method `user' for #<Comment:0x007fc94a46b340>

Posted in How do I make comments show the user who created it?

I would think that i could do something like the code below but it's not working. How can i approach this so it can show the user who created the comment and other information about the user? I did follow the tutorial video on polymorphic associations if that helps.

 - commentable.comments.each do |comment|
   .col-md-1
     .image 
        = image_tag comment.user.avatar.url(:medium)
   .col-md-11
     .username
        = comment.user.username
        .created_at
           = comment.created_at.strftime("%b, %d")
     .comment-body
        %p
          = comment.body

Posted in How would i go with making an inventory for products?

I'm getting more into products in a ecommerce store. Say i have shirts for sale and i want to keep track of how many i have and of how many i sell and ship. How would i keep a count of that?

Posted in How would i go with making an inventory for products?

I'm not sure if i'm saying it right but how can i manage inventory for my products? What are some steps that i have to take to get started?

Posted in Refile :fill does not work for me

I have the refile gem connected with aws by following the instruction on their github. For an image i would get something like

/attachments/store/78b928c7897e3b12de80cf1ffdfdfdfdf/image.jpg

which works but when i try to use the :fill that refile provides my images don't display. You can see that i have the fill/400/400 in the url below.

/attachments/store/fill/400/400/78b928c7897e3b12de80cf1ffdfdfdfdf/image.jpg

I'm guessing that i have to change the way aws stores the image in this code.

require "refile/backend/s3"

aws = {
  access_key_id: "xyz",
  secret_access_key: "abc",
  bucket: "my-bucket",
}
Refile.cache = Refile::Backend::S3.new(prefix: "cache", **aws)
Refile.store = Refile::Backend::S3.new(prefix: "store", **aws)

Posted in How to add records to has_many :through association

I do have it as that Chris but i guess i actually cant save it through a model or site i have to create it through the model_site Model.

I could have called my Model girl instead of model to not confuse anything but having the word Model just sounded better to me.

Posted in How to add records to has_many :through association

I did try that and it still does not save. I'm assigning the site_id and model_id which has to be saved by creating a new ModelSite. I need to find a way to save those id's when creating a new site or model.

Posted in How to add records to has_many :through association

I have three models called Site, Model and ModelSite as shown below. If i create a new model_site i can choose a site and model to save which works good but what i need help in is how can i choose a site or model if I'm creating a new site or model? I'm showing an example on how i create a new site which has a .association :sites input. I can pick the sites but it does not get saved.

class Site < ActiveRecord::Base
  has_many :model_sites, inverse_of: :site, dependent: :destroy
  has_many :models, through: :model_sites
end

class ModelSite < ActiveRecord::Base
 belongs_to :model
 belongs_to :site
 validates_presence_of :model, :site
end

class Model < ActiveRecord::Base
 has_many :model_sites, inverse_of: :model, dependent: :destroy
 has_many :sites, through: :model_sites
end

New site

<%= simple_form_for [:admin, @model] do |f| %>
 <%= f.input :name %>
 <%= f.association :sites, as: :check_boxes %>
<% end %>