我想知道是否有办法计算两个GPS坐标的距离而不依赖于Google Maps API.
我的应用程序可能会收到float中的坐标,或者我必须在地址上执行反向GEO.
我想知道如何从Rails获取当前周数,以及如何操作它:
谢谢.
我正在尝试从自身获取方法名称:
def funky_method
self.inspect
end
Run Code Online (Sandbox Code Playgroud)
它返回"main".
我怎样才能返回"funky_method"?
我正在尝试保存一个名为的变量persistent_data.
我通常使用session[:persistent_data]或cookies[:persistent_data],但我想使用localstorage代替.
我如何在Rails上做到这一点?
可能重复:
在Ruby中查找类的所有后代
所以,让我们说我们有:
class LivingBeing
class Animal
end
class Bacteria
end
class Virus
end
end
class Fungi < LivingBeing
end
Run Code Online (Sandbox Code Playgroud)
如何检查子类LivingBeing有哪些?我知道我们有Klass.ancestors什么方法可以看到与祖先相反的方法?
是否有可能使用delegate与has_many或has_one在一个多态模型关联?这是如何运作的?
class Generic < ActiveRecord::Base
...
belongs_to :generable, polymorphic: true
delegate :file_url, to: :image, allow_nil: true
delegate :type_cat, to: :cat, allow_nil: true
end
class Image < ActiveRecord::Base
...
has_one :generic, as: generable, dependent: :destroy
end
class Cat < ActiveRecord::Base
...
has_one :generic, as: generable, dependent: :destroy
end
Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails polymorphic-associations ruby-on-rails-3 ruby-on-rails-4
如果end_user无法访问应用程序的源代码,为什么我们仍然需要将某些方法设为私有?
我正在阅读使用Rails的Pragmatic Agile Web开发,我无法理解为什么我们需要将以下方法设为私有(即使在阅读了解释之后):
private
def current_cart Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
end
Run Code Online (Sandbox Code Playgroud)
它说它永远不会允许Rails将它作为一个动作提供,但作为一个编码器,为什么我自己会这样做呢?
我有一些应该非常简单的东西,但这会让我感到害怕.
l = LineItem.first
#<LineItem id: 5, product_id: 1, quantity: 1, price: #<BigDecimal:7f7fdb51a3f8,'0.999E3',9(18)>, cart_id: 5, discount_percentage: 10, discount_amount: nil, discount_active: true, created_at: "2012-01-12 16:17:41", updated_at: "2012-01-12 16:17:41">
Run Code Online (Sandbox Code Playgroud)
我有
l.discount_percentage.blank?
=> false
Run Code Online (Sandbox Code Playgroud)
所以,我有以下方法:
def total_price
discount_amount = 0 if discount_amount.blank?
discount_percentage = 0 if discount_percentage.blank?
discounted_amount_from_percent = price*(discount_percentage.to_f/100)
applicable_discount = [discount_amount,discounted_amount_from_percent].max
return (price-applicable_discount)
end
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时:
l.total_price
Run Code Online (Sandbox Code Playgroud)
而不是返回899,它返回999(意味着if discount_percentage.blank?根本不起作用!)
或者语法WHATEVER_HERE if true/false仅适用于View on Rails?
我正在创建一个名为Configuration的模型,我有以下代码,我想通过使用元编程使其更具动态性.
在配置模型的数据库表中,我有以下数据.
---------------------------------------------------------
variable_name as string | value in text
|
company_name | MyCompany
welcome_text | Welcome to MyCompany's App!
email_order_text | You've just created an account with MyCompany.
year_since | 2012
----------------------------------------------------------
class Configuration < ActiveRecord::Base
#nothing here yet
end
----------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
目前,访问company_name的唯一方法是在rails控制台中执行以下操作:
configuration_company_name = Configuration.find_by_variable_name("company_name")
configuration_company_name.company_name
> "MyCompany"
Run Code Online (Sandbox Code Playgroud)
我认为这是一种不可接受的做事方式.首先,每次有人检查公司名称时,它都会访问数据库.我想如果我可以在应用程序启动时加载它并且不必再次访问它,因为它在内存中,那么它会更好.我怎样才能做更动态的事情,所以我可以像这样访问"MyCompany"这个值.
Configuration.company_name
> "MyCompany"
Run Code Online (Sandbox Code Playgroud)
这样做的原因是允许快速定制应用程序.
我需要为Car和Store创建一个名为CarStoreTracker的连接模型,它们彼此之间有很多.
class Car < ActiveRecord::Base
has_many :carstoretrackers # It seems to work
has_many :stores, :through => :carstoretrackers # I bet the naming is not being recognized by Rails convention
end
class Store < ActiveRecord::Base
has_many :carstoretrackers # It seems to work
has_many :cars, :through => :carstoretrackers # Same issue
end
class CarStoreTracker < ActiveRecord::Base
belongs_to :store
belongs_to :car
end
Run Code Online (Sandbox Code Playgroud)
CarStoreTracker有
car_id and store_id on its table.
Run Code Online (Sandbox Code Playgroud)
当我跑:
> CarStoreTracker.first.car
> CarStoreTracker.first.store
Run Code Online (Sandbox Code Playgroud)
他们都工作.
但
Store.first.cars Car.first.stores Store.carstoretrackers Car.carstoretrackers
他们没有工作.NameError:未初始化的常量"CURRENTMODEL":: Carproducttracker
所以,我取消了CarProductTracker,我只使用了Tracker这个名称来模型,一切正常. …
ruby model ruby-on-rails naming-conventions has-many-through
我想知道Rails如何处理方法空白?
我试图建立自己的空白?等效的方法,但这并不容易.这是我的尝试:
def my_blank(state)
if state == nil or state.empty? == true
true
else
false
end
end
Run Code Online (Sandbox Code Playgroud) 我将mp3文件添加到/ public文件夹中,我发现了一些问题.
在我看来,我添加了链接:
= link_to "Interview 1", "/interview_01.mp3"
Run Code Online (Sandbox Code Playgroud)
当我从页面看到链接时,一切都显示正常,直到我点击它.浏览器挂起一分钟或更长时间,磁贴变为"未定义",我的谷歌浏览器冻结.
它出什么问题了?我怎样才能让人们可以下载文件?