开发者

rails many to many model query problem

there have two model Product & Gift ,here is the relationship

Product belongs to many Gifts one Gift category has many products

so they have a assocation table named gift_products

how can i write a code achieve this sql

select top 1 from gift_products where product_id=1 and gift_id=2

the meaning is i want check,is a specific record already in the assocation table,i'm new on rails ,

i tried use

rails g model GiftProduct
GiftProduct.find_by_sql("select top 1 from gift_products where pr开发者_Go百科oduct_id=1 and gift_id=2")

to generate a model to treat assocation table as operate like usual,but it seems dosen't work, i use sqlite3 as dev db .


Is this what you want?

GiftProduct.where(:product_id => 1, :gift_id => 1).first

return nil if no such record, otherwise the gift_product record.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜