Skip to content

Instantly share code, notes, and snippets.

@karmiclychee
Last active December 16, 2015 13:19
Show Gist options
  • Save karmiclychee/5441070 to your computer and use it in GitHub Desktop.
Save karmiclychee/5441070 to your computer and use it in GitHub Desktop.
class Collection < ActiveRecord::Base
belongs_to :profile
has_many :projects
has_one :thumbnail, class_name: "item", as: :thumbnailable
attr_accessible :name, :description
attr_accessor :image
validates :name, uniqueness: { scope: :profile_id }, length: { minimum: 1, maximum: 100 }, allow_blank: false
validates :description, length: { minimum: 5, maximum: 1000 }, allow_blank: true
def image
unless self.projects == nil
self.projects.first.image
else
nil
end
end
end
class Project < ActiveRecord::Base
belongs_to :collection
has_many :items
has_one :item, as: :image
attr_accessible :name, :description, :items, :links
attr_accessor :image
serialize :links
validates :name, uniqueness: { scope: :collection_id }, length: {minimum: 1, maximum: 20}, allow_blank: false
validates :description, length: {minimum: 5, maximum: 200}, allow_blank: true
def image
unless self.items == nil
self.items.first.image
else
nil
end
end
end
Trace with raise self.id.inspect - ln 18
ActionView::Template::Error (undefined method `id' for #<#<Class:0x00000006b914b8>:0x007f26ec91f518>):
15: - gallery.each do |gallery_item|
16: %li.span2{ id: "#{new_sibling}_#{gallery_item.id}" }
17: .thumbnail
18: - raise self.id.inspect
19: - if gallery_item.image
20: = image_tag gallery_item.image.url(:medium), { class: ["img-rounded"] }
21: - else
app/views/layouts/_gallery.html.haml:18:in `block in _app_views_layouts__gallery_html_haml__1575251927046492898_56927680'
app/views/layouts/_gallery.html.haml:15:in `_app_views_layouts__gallery_html_haml__1575251927046492898_56927680'
app/views/users/show.html.haml:1:in `_app_views_users_show_html_haml__2844021620757440522_48447860'
--------------------------------------------
Started GET "/" for 127.0.0.1 at 2013-04-22 22:13:36 -0700
Processing by UsersController#show as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
Profile Load (0.2ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = 2 LIMIT 1
Collection Load (0.2ms) SELECT "collections".* FROM "collections" WHERE "collections"."profile_id" = 2
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."collection_id" = 7
Item Load (0.1ms) SELECT "items".* FROM "items" WHERE "items"."project_id" = 2
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."collection_id" = 8
Item Load (0.1ms) SELECT "items".* FROM "items" WHERE "items"."project_id" = 3
Rendered layouts/_gallery.html.haml (9.3ms)
Rendered users/show.html.haml within layouts/application (9.9ms)
Completed 500 Internal Server Error in 14ms
ActionView::Template::Error (undefined method `image' for nil:NilClass):
15: - gallery.each do |gallery_item|
16: %li.span2{ id: "#{new_sibling}_#{gallery_item.id}" }
17: .thumbnail
18: - if gallery_item.image
19: = image_tag gallery_item.image.url(:medium), { class: ["img-rounded"] }
20: - else
21: %img{:src => "/assets/empty.jpg", :alt => "generic"}/
app/models/project.rb:15:in `image'
app/models/collection.rb:13:in `image'
app/views/layouts/_gallery.html.haml:18:in `block in _app_views_layouts__gallery_html_haml___3765302753760464732_70071251215420'
app/views/layouts/_gallery.html.haml:15:in `_app_views_layouts__gallery_html_haml___3765302753760464732_70071251215420'
app/views/users/show.html.haml:1:in `_app_views_users_show_html_haml___3576415342936073813_65436800'
Trace with raise self.id.inspect - ln 18
ActionView::Template::Error (undefined method `id' for #<#<Class:0x00000006b914b8>:0x007f26ec91f518>):
15: - gallery.each do |gallery_item|
16: %li.span2{ id: "#{new_sibling}_#{gallery_item.id}" }
17: .thumbnail
18: - raise self.id.inspect
19: - if gallery_item.image
20: = image_tag gallery_item.image.url(:medium), { class: ["img-rounded"] }
21: - else
app/views/layouts/_gallery.html.haml:18:in `block in _app_views_layouts__gallery_html_haml__1575251927046492898_56927680'
app/views/layouts/_gallery.html.haml:15:in `_app_views_layouts__gallery_html_haml__1575251927046492898_5
Original exception tripped on line 19 of the above. "image" no method error. This collection currently has no Projects.
class Collection < ActiveRecord::Base
belongs_to :profile
has_many :projects
has_one :thumbnail, class_name: "item", as: :thumbnailable
attr_accessible :name, :description
attr_accessor :image
validates :name, uniqueness: { scope: :profile_id }, length: { minimum: 1, maximum: 100 }, allow_blank: false
validates :description, length: { minimum: 5, maximum: 1000 }, allow_blank: true
def image
unless self.projects = nil
self.projects.first.image
else
nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment