Where would I put this include? I tried putting it at the very top of my
model file, but when I opened up the rails console, and tested it out
using:
m = Sellers.new("hello")
I get this output:
NameError: uninitialized constant Sellers
from
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:443:in
`load_missing_constant'
from
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in
`const_missing'
from
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:92:in
`const_missing'
from (irb):1
I put the gem in my environment.rb file as well. Any help is
appreciated!
'include' statements usually go inside a class -- essentially,
'include Amatch' adjusts the class you're defining by adding the
methods from the Amatch module.
A 'require' statement, on the other hand, just tells Ruby to bring in
the contents of the filename you specify.
So you probably want this kind of thing:
require 'amatch'
class Seller
include Amatch
# Rest of your class definition here
end
Try that and see if it resolves the error. If not, there may be a
problem finding the amatch file itself.
Thank you for the reply. putting the include under the model class,
which worked! Thanks very much for that.
In my script/console, I tried instantiating a new "Sellers" object
(Sellers is one of the types of edit distance methods), with this code:
m = Sellers.new("hello")
but still got the same error output.
however, I created a new object of my model, Responses:
x = Responses.new
after which I was able to create an amatch Sellers object within a
function:
def title_must_have_changed_by_one_letter
m = Sellers.new(first_response)
errors.add_to_base("Sorry, the last submitted response must have
changed by one letter") if m.match(second_response) != 1.0
end
That makes sense to me. I'm just curious why I wasn't able to make a new
Sellers object in the first page?
Did you restart or reload your console after making the change to
seller.rb? Sometimes there are changes that the console doesn't pick
up until you reload it.
Oh, I don't have a Seller model. The Seller object I was trying to
instantiate was an amatch class inside of my Response model. Sorry for
the confusion.
I guess what I'm trying to say is: is there any way to instantiate the
amatch Seller class in the console, after you have create a new Response
object (which is the model - response.rb)?
Also, I tried accessing my views in my browser and got this error:
NameError in Games#show
Showing app/views/games/show.html.erb where line #8 raised:
uninitialized constant Response::Amatch
Extracted source (around line #8):
Thank you for your help, that worked! I put that extra code in the
model, but I still get the uninitialized constant error in my browser
when accessing the show action. Not sure what may be the issue.
If I’m understanding you correctly, you want to load up script/console (or rails console), instantiate a Seller object, and then access it from the browser? I’m not sure you can do that, because your console is a separate instance of the application (it may access the same database but it’s running in a different location in memory if I understand it correctly).
If you’re just trying to get it to work in your application (no console), then where are you putting require 'amatch'? Somewhere all of the parts of your app need it, I hope!
Sorry, I was actually asking two different questions in my last reply.
You've answered my question about the console, thanks for your help!
It's much clearer now.
I've put the " include 'amatch' " in both my Post model and my Response
model, which have a one to many relationship. However, I get this error
message:
NameError in Posts#show
Showing app/views/posts/show.html.erb where line #8 raised:
uninitialized constant Response::Amatch
here's my code for the post model:
class Post < ActiveRecord::Base
include Amatch
has_many :responses
validates_presence_of :title
validates_length_of :title, :within => 3..10
end
and the Response model:
class Response < ActiveRecord::Base
include Amatch
belongs_to :post
validates_uniqueness_of :title
validates_presence_of :title
validates_length_of :title, :within => 3..5
validate :title_must_have_changed_by_one_letter
protected
def title_must_have_changed_by_one_letter
m = Amatch::Sellers.new(title)
errors.add_to_base("Sorry, you must change the last submitted string
by one letter") if m.match(self.response.last) != 1.0
end
end
Would I need to include Amatch in the controller and views as well?
I’m unable to duplicate your problem with Rails 2.3.5.
I just learned that config.gem "amatch" automatically requires the library with the same name, so you shouldn’t need to put require "amatch" anywhere in the application. Same goes for Rails 3 and Bundler.
I just whipped up a simple scaffold application, here’s what it looks like (Maybe this will help(?)–I’m not very familiar with how Amatch works, maybe I’ll look into that):
Thanks Philip. After reading your response, I tried doing another dummy
application myself, and in the middle of getting everything set up, I
realized that I didn't restart the script/server after adding the
include. So I went back to my other application, and this worked.
Occam's razor :-).
Sorry to hound you with additional questions, but I was wondering if you
had time for one more.
I'm having a hard time limiting the scope of new comments to the Post
model. So, for validates_uniqueness_of :title in the comment.rb model,
that uniqueness would only be attached to the post I'm commenting to,
not globally among all comments in the application.
I tried putting this in comment.rb:
validates_uniqueness_of :title, :scope => :post
but got this error message in my terminal:
Processing CommentsController#create (for 127.0.0.1 at 2010-08-16
21:28:32) [POST]
Parameters: {"comment"=>{"title"=>"rent"}, "commit"=>"Submit Your
Comment",
"authenticity_token"=>"z5AzsFgaJPdvGiTyy3Yeyb8f0RV+dmpdw5PanAadjOw=",
"game_id"=>"5"}
Post Load (0.2ms) SELECT * FROM "posts" WHERE ("posts"."id" = 5)
CACHE (0.0ms) SELECT * FROM "posts" WHERE ("posts"."id" = 5)
Response Load (0.0ms) SQLite3::SQLException: no such column:
comments.posts: SELECT "comments".id FROM "comments" WHERE
("comments"."title" = 'rent' AND "comments".post = 5) LIMIT 1
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column:
comments.post: SELECT "comments".id FROM "comments" WHERE
("comments"."title" = 'rent' AND "comments".post = 5) LIMIT 1):
app/controllers/comments_controller.rb:4:in `create'