Weird and inconsistent Model issues
I feel like I'm missing something rather important in both circumstances, but I can't seem to figure either out:
1) I have a model named TestCase -
class TestCase < ActiveRecord::Base
belongs_to :test_suite
scope :queued, lambda { where("test_cases.suite_id IS NOT NULL") }
scope :assigned_to, lambda { |sid| where(:suite_id => sid) }
end
The controller can interact with it perfectly fine. When trying to display information from it in either the view or via the view helper such as TestCase.all
, I get a NoMethodError (undefined method 'all')
If I call it with ::TestCase.all
, that works. I had a theory that it has something to do with the fact that it's associated to another model (belongs_to ...), I just can't find anything to confirm that or tell me why that happens.
2) On another project I have yet another model named Artwork. Again, it has associations (belongs_to). In this case, I can access it just fine in the view, and all the methods within it work fine for the controller except if I try to do dynamic method calls. In this case I ha开发者_如何学JAVAve a simple toggle for -
@artwork = Artwork.find(params[:id])
value = params[:value].to_sym
@artwork.update_attributes(value => !@artwork.method(value).call)
That gives me a NoMethodError
. However, if I add - if @artwork.respond_to?(value)
- then it works as expected. Again, I can't figure out why.
Both items I get working using the mentioned methods, but again, I feel like I'm really missing something important here.
Re: problem 1 -- Don't call your model "TestCase". That conflicts with the Rails TestCase
class.
Re: problem 2 -- That's an odd way of doing things. You might get it working by using
@artwork.send(value)
but keep in mind that a rogue user could pass in any method name through the form and wreak havoc.
精彩评论