开发者

creating Models with sqlite3 + datamapper + ruby

How do you build a model with the following associations (i tried but couldn't get it to work):

each Order has: a Customer, a SalesRep, many OrderLine that each has an item.

I tried: when I do: Customer.all(Customer.or开发者_C百科ders.order_lines.item.sku.like => "%BLUE%") the output is :[]

instead of: '[#<"Customer @id=1 @name="Dan Kubb">]'

When I delete SalesRep: it works.

Customer

has n, :orders

has n, :items, :through => :order

SalesRep

has n, :orders

has n, :items, :through => :order

Order

belongs_to :customer

belongs_to :technician

has n, :order_lines

has n, :items, :through => :order_line

OrderLine

belongs_to :order

belongs_to :item

Item

has n, :order_lines


Since the output isn't an error, it could be that you don't actually have the data in your database.

Try the following with irb -r your_models_file.rb: - c = Customer.create(:name => "Dan Kubb")

  • o = Order.new(:customer => c) # Create and add technician unless it's :required => false
  • i = Item.create(:sku => "BLUE") # Plus any other required fields
  • ol = OrderLine.create(:order => o, :item => i)
  • o.order_lines << ol; o.save

That should create the records needed for this to work. Try this out, and if it doesn't work, post your entire models file so we can get a better idea of what's up.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜