Reputation: 623
i have the following Problem, i Have the following in my customer bill view
<%= f.collection_select :product_id,Product.all,:id,:name %>
This is getting list of all the products from "Product" model and giving option to select from it. But i want to select the list of products from the "StoreOpeningStock" model.
I have these in my model
class Product< ActiveRecord::Base
has_many :store_opening_stocks
has_many :customer_bills
attr_accessible :name
end
class StoreOpeningStock < ActiveRecord::Base
attr_accessible :product_id
belongs_to :product
end
class CustomerBill < ActiveRecord::Base
attr_accessible :product_id
belongs_to :product
accepts_nested_attributes_for :store_opening_stock
end
Can anyone guide me how i can get product name and id from store_opening_stock??? Should i use Helpers??? or is there any other way?? Thanks in advance
I tried using helpers
def getting_prod_names
@sto = StoreOpeningStock.all
for x in @sto
[
['{x.product.title}', '{x.product_id}']
]
end
end
getting following output
<%= f.select :product_id, options_for_select(getting_prod_names) %>
ANy Help?? :)
Upvotes: 4
Views: 1103
Reputation: 3456
I'd add a scope to your products model:
class Product< ActiveRecord::Base
has_many :store_opening_stocks
has_many :customer_bills
attr_accessible :name
scope :having_store_opening_stocks, :joins => : store_opening_stocks, :select => 'distinct product.*', :conditions => 'store_opening_stocks.product > 0'
end
Then you can use Product.all.having_store_opening_stocks to select only products with such stocks, for example:
<%= f.select :product_id, Product.having_store_opening_stocks.map { |product| [product.name, product.id] } %>
Upvotes: 1
Reputation: 10769
You need to clarify the relationship between your models...
But just to give you an idea. You can define the collection of products you want to display in your controller
, inside the action
related to the view (where you are displaying the collection).
Controller:
@products= #here you should call all products you want
Then, your collection of products can be displayed like:
<%= f.collection_select :product_id, @products,:id,:name %>
EDIT
You need to revise the relationship between your models. A product
has many customer_bills
, but are you sure that each customer_bill
belongs to a single product
?
I think you have a many-to-many relationship, as a customer_bill
can also have many products
.
If I understand it right, the solution is to create a ProductLine
model between this many-to-many relationship.
Also, what is the difference between Product
and StoreOpeningStock
? What attributes have you included in the StoreOpeningStock
?
If you have created this model only to show the availability of products, why don't you add an attribute in the Product
model, for example a boolean column called availability
.
Upvotes: 3
Reputation: 20639
So you want to find all products that have a StoreOpeningStock. This is solely a model concern and have nothing to do with helpers.
class Product
# Find all products that have a StoreOpeningStock
def self.in_stock
find(StoreOpeningStock.product_ids)
end
end
class StoreOpeningStock
# Collect all product ids from stocks
def self.product_ids
uniq.pluck(:product_id)
end
end
Now you can use Product.in_stock instead of Product.all to have the only ones in stock.
Upvotes: 1
Reputation: 8954
When you create a form the data used to ccreate a collection_select
isnt limited to the Class your going to create an object for. You could simply do the following:
<%= f.collection_select :product_id,StoreOpeningStock.all,:product_id ,:name %>
This should to it for you,...
add this to your StoreOpeningStock
class:
def name
return self.product.name unless self.product.nil?
""
end
Upvotes: 3