如何在 rails 库中包含多个模型
How to include multiple model in rails lib
在我的 /lib 中我有下面这个 class:
module Application
class Post < ActiveRecord::Base
attr_accessor :id
def artilce_content
post.articles.content
end
private
def post
Post.find(id)
end
end
end
但问题是 article
未定义。
NoMethodError: undefined method ziptag_type' for #<Application::Api::V2::Ziptag:0x00000008edc120>
问题是,如何在 /lib 中使用或包含多个模型?我尝试在 module
上方添加 class User < ActiveRecord::Base; end
class Article < ActiveRecord::Base; end
module Application
class Post < ActiveRecord::Base; end
:
:
:
end
end
但是没有用。
我找到答案了。应该是:
class Article < ActiveRecord::Base; attr_accessor :column1, :other_column end
不是:
class Article < ActiveRecord::Base; end
现在,它在我身上运行良好。
这里最好的方法是使用 require。
require 'your_model'
在你的情况下,应该是:
require 'article'
module Application
class Post < ActiveRecord::Base
attr_accessor :id
def artilce_content
post.articles.content
end
private
def post
Post.find(id)
end
end
end
在我的 /lib 中我有下面这个 class:
module Application
class Post < ActiveRecord::Base
attr_accessor :id
def artilce_content
post.articles.content
end
private
def post
Post.find(id)
end
end
end
但问题是 article
未定义。
NoMethodError: undefined method ziptag_type' for #<Application::Api::V2::Ziptag:0x00000008edc120>
问题是,如何在 /lib 中使用或包含多个模型?我尝试在 module
class User < ActiveRecord::Base; end
class Article < ActiveRecord::Base; end
module Application
class Post < ActiveRecord::Base; end
:
:
:
end
end
但是没有用。
我找到答案了。应该是:
class Article < ActiveRecord::Base; attr_accessor :column1, :other_column end
不是:
class Article < ActiveRecord::Base; end
现在,它在我身上运行良好。
这里最好的方法是使用 require。
require 'your_model'
在你的情况下,应该是:
require 'article'
module Application
class Post < ActiveRecord::Base
attr_accessor :id
def artilce_content
post.articles.content
end
private
def post
Post.find(id)
end
end
end