ruby - Rails 3: use contents of an array as variables in a where method -
i have 3 models: isbn, sale , channel.
aim: list in isbns show.html.erb view looks this:
isbn: myisbn
total sales myisbn: 100
myisbn sales channel 1: 50
myisbn sales channel 2: 25
myisbn sales channel 3: 25
here models. isbn.rb model
has_many :sales has_many :channels, :through => :sales
sale.rb model (has attributes sales_channel_id, isbn_id, quantity)
has_many :channels belongs_to :isbn
channel.rb model:
belongs_to :sale
i've been working in isbns controller, in show method, work. thought i'd refactor later - advice on whether of stuff should go in model welcome.
so far i've got this:
@channelisbn = sale.where("sales_channel_id =?',1).where("isbn_id=?",3) @channelsalesisbn = 0 @channelisbn.each {|y| @channelsalesisbn =+ y.quantity}
this gets sales channel id 1 , isbn id 3. it's not use, ids hard coded. got channel ids array:
@channellist = channel.all @channel = 0 @channelarray = @channellist.map {|z| @channel = z.id}
which gives me lovely array of [1,2,3,4]
but can't figure out how pass 1, 2, 3 , 4 block can used isbn's sales have sales channel id. tried (still hardcoding isbn id - thought i'd tackle 1 problem @ time), returned empty array:
@channelarray.each |channel| @channelisbn = [] @channelisbn = sale.where("sales_channel_id = ?", channel).where("isbn_id = ?",3) @channelsalesisbn = 0 @result = [] @result << @channelisbn.each {|a| @channelsalesisbn =+ a.quantity} end
i going sum contents of array.
any gratefully received. first post, 0 acceptance rate change soon!
update
just finish question off, here's i've ended up, great, , ready tinkering with: array, nicely grouped, giving me sales isbn channel. group_by tip off!
#in show action in isbns controller: @isbn = isbn.find(params[:id]) @channelarray = channel.select(:id).all @channelarray.group_by {|i| sale.where("channel_id = ?",i).where("isbn_id =?", @isbn)}
from console, line breaks added clarity:
(sneakily set @isbn = 3 first of all, since in console can't pass params view, @isbn instance defined in controller nil in console)
ruby-1.9.2-p180 :067 > @channelarray.group_by {|i| sale.where("channel_id = ?",i).where("isbn_id =?", @isbn)} => {[#<sale id: 1, isbn_id: 3, quantity: 10000, value: 12000, currency: "gbp", total_quantity: nil, created_at: "2011-05-06 12:30:35", updated_at: "2011-05-07 17:43:13", customer: "waterstone's", retail_price: nil, discount: nil, invoice_date: "2011-05-24">, #<sale id: 2, isbn_id: 3, quantity: 1000, value: 500, currency: "gbp", total_quantity: nil, created_at: "2011-05-07 09:37:53", updated_at: "2011-05-07 19:14:52", customer: "borders", retail_price: nil, discount: nil, invoice_date: "2011-02-05">]=>[#<channel id: 1>], [#<sale id: 3, isbn_id: 3, quantity: 500, value: 1500, currency: "", total_quantity: nil, created_at: "2011-05-07 09:38:11", updated_at: "2011-05-07 19:15:07", customer: "borders", retail_price: nil, discount: nil, invoice_date: "2011-12-05">, #<sale id: 4, isbn_id: 3, quantity: 45, value: 300, currency: "", total_quantity: nil, created_at: "2011-05-07 09:38:38", updated_at: "2011-05-07 19:15:36", customer: "borders", retail_price: nil, discount: nil, invoice_date: "2011-06-05">]=>[#<channel id: 2>], []=>[#<channel id: 3>], []=>[#<channel id: 4>]}
update 2
ha, hash generated had key value pairs wrong way round. array containing sales data key - should have been value. rubydocs saved day:
@salesbychannel = @salesbychannelwrong.invert
the invert
method switches key-value pairs. sweet.
what you're looking passing array arel#where(), this:
sale.where(:sales_channel_id => @channelarray)
this should execute in query. if that's not working, can pass array activerecord#find, this:
sale.find(@channelarray)
hope helps
Comments
Post a Comment