开发者

Rails 3 - Seed.rb data for Money Class

开发者 https://www.devze.com 2023-02-20 13:47 出处:网络
I am trying out seeds.rb for the first time, and one of my data models uses encapsulation provided by the money gem.

I am trying out seeds.rb for the first time, and one of my data models uses encapsulation provided by the money gem.

Relevant gems:

money (3.6.1)
rails (3.0.5)

My model thus far:

app/models/list.rb
class List < ActiveRecord::Base
  attr_accessible :alias, :unit, :participating_manufacturer, :quantity
                  :latest_price_cents, :latest_price_currency, :url
  belongs_to :user

composed_of :latest_price,
  :class_name => "Money",
  :mapping => [%w(latest_price_cents latest_price_cents), %w(latest_price_currency currency_as_string)],
  :constructor => Proc.new {
    |latest_price_cents, latest_price_currency| Money.new(latest_price_cents ||
    0, latest_price_currency || Money.default_currency)
  },
  :converter => Proc.new {
    |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentEr开发者_StackOverflowror,
    "Can't convert #{value.class} to Money")
  }
end

1) (Addressed successfully)

2) When I get to writing validations, would it be best to write them for the :latest_price attribute or for the :latest_price_cents & :latest_price_currency attributes seperately?

/db/seeds.rb
users = User.create([{ :name => "Foo", :email => "foo@gmail.com",
                   :password => "foobar", :password_confirmation => "foobar" }])
# etc, will add more users to the array
list = List.create(:user_id => users.first.id, :alias => "Januvia 100mg",
                   :unit => "tablet", :participating_manufacturer => "Merck",
                   :quantity => 30, :latest_price_cents => 7500,
                   :latest_price_currency => "USD", :url =>
                   "http://www.foobar.com/januvia/100mg-tablets/")

3) Perhaps it is minutiae, but in the seed, should I be assigning values to the virtual :latest_price attribute or to the latest_price_cents and latest_price_currency attributes directly? Is there any way to use faker rather than /db/seeds.rb to perform this task?

I am new to rails and web development.


I can't see your latest_price attribute anywhere, so I'm not sure how to answer your question. Generally, you should validate the attributes entered in the user form. So if a user enters latest_price_cents and latest_price_currency in a form, then they're the ones which need validating.

There's a bug in your seed file. You want to pass in a hash, not an array, when creating a new user; and users should be an array.

users = []
users << User.create!(:name => "Foo",
                     :email => "foo@gmail.com",
                     :password => "foobar",)
                     :password_confirmation => "foobar")

However, if you're considering faker because you want to create some dummy data, take a look at Machinist or Factory Girl. They're designed for creating dummy data, normally for automated tests.

Once you've set up some blueprints, if you want to create dummy data in your seeds file, you can do something like this in seeds.rb:

20.times { List.make } unless Rails.env.production?
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号