This has to be another one of those really easy obvious ones, but I just
can’t figure it out…
I have a boolean field called “public” to designate whether a record in
my database should be viewable by the public, or only by people signed
in.
In my controller, I have:
------------app/controllers/sources_controller.rb--------------
class SourcesController < ApplicationController
…
def find_public
Source.find_by_live(true)
end
def find_private
Source.find_by_live(false)
end
end
Then, in my view, I have:
-------------app/views/users/show.html.erb---------------------
<% @user.sources.public do |c| %>
<%= c.editor %>,
<%= link_to c.title, :controller => “sources”, :action =>
“show_cat”, :id => c.id %>
(<%= c.publication %>, <%= c.pub_date %>).
<%= link_to “Edit”, {:controller => “sources”, :action =>
‘edit’, :id => c.id} %> |
<%= link_to “Delete”, {:controller => “sources”, :action =>
‘delete’, :id => c.id},
:confirm => “Are you sure you want to delete this record
permanently?” %>
<% end %>
When I run this, I get a no method error: “undefined method
`find_public’”
It doesn’t work in the console either:
Source.find_private
NoMethodError: undefined method find_private' for #<Class:0x2f36a18> from /usr/pkg/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1667:inmethod_missing’
from (irb):1
I know this is something simple, but I can’t figure it out.
Thanks so much for your help!
Morgan.
On Aug 19, 12:07 am, Morgan K. [email protected]
wrote:
When I run this, I get a no method error: “undefined method
`find_public’”
It doesn’t work in the console either:>> Source.find_private
NoMethodError: undefined method `find_private’ for #Class:0x2f36a18
from
Well you’ve defined an instance method on SourcesController, but
you’re trying to call a class method on Source instead. Not sure how
you were expecting that to work. public is a magic word in ruby but
you might be able to get away with it here.
Fred
Well you’ve defined an instance method on SourcesController, but
you’re trying to call a class method on Source instead. Not sure how
you were expecting that to work. public is a magic word in ruby but
you might be able to get away with it here.
Yes, I’m new to all of this, and I knew I was doing it wrong… But can
you help me figure out how to do it right?
Frederick C. wrote:
public is a magic word in ruby but
you might be able to get away with it here.
Fred
Right, the boolean field is called “live”, not “public”… sorry for
that confusion…
On Aug 19, 12:22 am, Morgan K. [email protected]
wrote:
Well you’ve defined an instance method on SourcesController, but
you’re trying to call a class method on Source instead. Not sure how
you were expecting that to work. public is a magic word in ruby but
you might be able to get away with it here.
Yes, I’m new to all of this, and I knew I was doing it wrong… But can
you help me figure out how to do it right?
Well if you want to be able to say Source.find_private then you need
class Source < ActiveRecord::Base
def self.find_private
…
end
end
or (equivalently)
class Source < ActiveRecord::Base
class << self
def find_private
…
end
end
end
There’s a slightly magic incantation needed to make a class method,
but if it’s not obvious why defining find_private on SourcesController
is completely nonsensical then I suggest you read up on you object
oriented programming (something like the pickaxe will introduce you to
that and ruby at the same time)
Fred