Skip to content

Instantly share code, notes, and snippets.

@mkwiatkowski
Created January 18, 2014 07:56
Show Gist options
  • Save mkwiatkowski/8487607 to your computer and use it in GitHub Desktop.
Save mkwiatkowski/8487607 to your computer and use it in GitHub Desktop.
diff --git a/app/controllers/hashtags_controller.rb b/app/controllers/hashtags_controller.rb
index f67091b..29d121a 100644
--- a/app/controllers/hashtags_controller.rb
+++ b/app/controllers/hashtags_controller.rb
@@ -1,7 +1,6 @@
class HashtagsController < ApplicationController
def index
- @hashtags = Hashtag.all
- @hashtag_groups = @hashtags.group_by { |t| t.tag }
+ @hashtags = Hashtag.all
end
def show
diff --git a/app/models/hashtag.rb b/app/models/hashtag.rb
index 97deb01..5fcd9d5 100644
--- a/app/models/hashtag.rb
+++ b/app/models/hashtag.rb
@@ -1,9 +1,10 @@
class Hashtag < ActiveRecord::Base
attr_accessible :tag, :user
- belongs_to :user
+ belongs_to :user # remove
has_many :categorizes
has_many :bookmarks, :through => :categorizes
validates :tag, presence: true
+ # add validate uniqueness
end
diff --git a/app/views/hashtags/index.html.haml b/app/views/hashtags/index.html.haml
index 7d1714d..c5d8e07 100644
--- a/app/views/hashtags/index.html.haml
+++ b/app/views/hashtags/index.html.haml
@@ -1,9 +1,9 @@
%h1 Hashtags#index
-- @hashtag_groups.each do |tag, collection|
+- @hashtags.each do |hashtag|
%p
- = link_to tag, " " # collection_link(collection) I need to somehow link to the hashtag show action and pass in the scoped collection to the controller!!
+ = link_to hashtag.tag, hashtag_path(hashtag)
%ul
- - collection.each do |hashtag|
+ - hashtag.bookmarks.limit(8).each do |bookmark|
%li
- = hashtag.bookmarks.first.url
+ = bookmark.url
diff --git a/db/schema.rb b/db/schema.rb
index 46ea66b..bb41a59 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -35,7 +35,7 @@ ActiveRecord::Schema.define(:version => 20140116071551) do
t.string "tag"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
- t.integer "user_id"
+ t.integer "user_id" # remove
end
add_index "hashtags", ["user_id"], :name => "index_hashtags_on_user_id"
diff --git a/db/seeds.rb b/db/seeds.rb
index e459811..cba2475 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -23,20 +23,20 @@ end
# #Inner iterator - Create 10 bookmarks per user -
10.times do # - create 10 new bookmarks for every user
bm = u.bookmarks.create(
- url: Faker::Internet.url,
- title: Faker::Lorem.words(rand(1..4)).join(" "),
- description: Faker::Lorem.paragraph(1)
- ) # - create a bookmark within the scope of a unique user instance
+ url: Faker::Internet.url,
+ title: Faker::Lorem.words(rand(1..4)).join(" "),
+ description: Faker::Lorem.paragraph(1)
+ ) # - create a bookmark within the scope of a unique user instance
bm.update_attribute(:created_at, Time.now - rand(600..31536000)) # set the created_at to a time within the past year
rand(1..4).times do
ht_insert = ht.sample
- hasht = bm.hashtags.create(tag: ht_insert)
- hasht.update_attribute(:user, u)
+ hasht = bm.hashtags.find_or_create(tag: ht_insert)
+ hasht.update_attribute(:user, u) # remove
end
end
end
-
+
puts "Seed finished"
puts "#{User.count} users created"
puts "#{Bookmark.count} bookmarks created"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment