Samuel
2010-Jun-02 15:49 UTC
Has many_through, how to show only unassociated fields for a category?
Hi everybody, I''m using ruby 1.9.1, with rails 2.3.*. I have an application of products and categories, but I want each type of product to have specific attributes. So, basically, according to the chosen category, I want different fields to be shown for the user. So, before actually making the association of the product with the attribute, I want to create an administration system where the administrators could actually select wich fields they want for each category, in an N:N association, usign has many through (categoryFields is the middle table). So, up to now, the structure is: class Field < ActiveRecord::Base has_many :categoryFields, :dependent => :destroy has_many :categories, :through => :categoryFields end class CategoryField < ActiveRecord::Base belongs_to :field belongs_to :category end class Category < ActiveRecord::Base has_many :categoryFields, :dependent => :destroy has_many :fields, :through => :categoryFields end And this works perfectly. so, now, in my Category Edit view, I want to show a list of ALL AVAILABLE FIELDS (i.e. only those not already associated to that specific category) at the right, and in the left, all the ASSOCIATED FIELDS for that category. Using a simple drag-and-drop, the user can drag fields from the right to insert them in category, or move them to the right to remove them from that specific category. The problem is that I don''t want that any field in the left column also appear in the right column (each field can only be used once in that specific category, since those attributes are unique). So, once you have associated a field to a category, it will not appear in the list of available fields (right list). I''m coming from PHP and this is my first rails application (I know, it''s kinda complicated for a noob). So, what should I do, to exclude those values? The edit method in the category controller: @category = Category.find(params[:id]) @categories = Category.roots #the categories acts as nested set, so we have to fetch all available parent categories @fields = Field.all Now, the view do the following: <div class="category_fields span-12 last"> <% if !@category.fields.empty? # this loops through all teh ASSOCIATED fields and display their names%> <% @category.fields.each do |field| %> <%=field.name%> <% end %> <% end %> </div> <div class="fields_available"> <% if !@fields.empty? # this loops through ALL THE fields and display their names %> <% @fields.each do |field| %> <%=field.name%> <% end %> <% end %> </div> So, where should I place the "filter" so that only available fields will appear in the second div? Shoud I place it in the controller, as a condition somehow, and how? Or should I use some array-function to "pop out" the already associated fields from the @fields array? Wich is better from the point of view of performance, and how do I do each of them? Thanks a lot in advice, for any help Samuel Brandão web-z6zVtI1jGl+AeuBi4663418n826c+h2f@public.gmane.org -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sharagoz
2010-Jun-03 12:34 UTC
Re: Has many_through, how to show only unassociated fields for a category?
Fields.all gives you an array of all the fields in the database @category.fields gives you an array of all the fields for the given category (since you have the has_many :fields, :through => :categoryFields association) How do you get the fields that are present in the first array but not in the second? Subtraction. @fields_not_chosen_yet = Fields.all - @category.fields Btw, you may want to exchange :categoryFields for :category_fields. On Jun 2, 5:49 pm, Samuel <gb.sam...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi everybody, > > I''m using ruby 1.9.1, with rails 2.3.*. I have an application of > products and categories, but I want each type of product to have > specific attributes. So, basically, according to the chosen category, > I want different fields to be shown for the user. > > So, before actually making the association of the product with the > attribute, I want to create an administration system where the > administrators could actually select wich fields they want for each > category, in an N:N association, usign has many through > (categoryFields is the middle table). So, up to now, the structure is: > > class Field < ActiveRecord::Base > has_many :categoryFields, :dependent => :destroy > has_many :categories, :through => :categoryFields > end > > class CategoryField < ActiveRecord::Base > belongs_to :field > belongs_to :category > end > > class Category < ActiveRecord::Base > has_many :categoryFields, :dependent => :destroy > has_many :fields, :through => :categoryFields > end > > And this works perfectly. > so, now, in my Category Edit view, I want to show a list of ALL > AVAILABLE FIELDS (i.e. only those not already associated to that > specific category) at the right, and in the left, all the ASSOCIATED > FIELDS for that category. Using a simple drag-and-drop, the user can > drag fields from the right to insert them in category, or move them to > the right to remove them from that specific category. > > The problem is that I don''t want that any field in the left column > also appear in the right column (each field can only be used once in > that specific category, since those attributes are unique). So, once > you have associated a field to a category, it will not appear in the > list of available fields (right list). I''m coming from PHP and this is > my first rails application (I know, it''s kinda complicated for a > noob). > > So, what should I do, to exclude those values? > > The edit method in the category controller: > @category = Category.find(params[:id]) > @categories = Category.roots #the categories acts as nested set, so > we have to fetch all available parent categories > @fields = Field.all > > Now, the view do the following: > <div class="category_fields span-12 last"> > <% if !...@category.fields.empty? # this loops through all teh > ASSOCIATED fields and display their names%> > <% @category.fields.each do |field| %> > <%=field.name%> > <% end %> > <% end %> > </div> > <div class="fields_available"> > <% if !...@fields.empty? # this loops through ALL THE fields and > display their names %> > <% @fields.each do |field| %> > <%=field.name%> > <% end %> > <% end %> > </div> > > So, where should I place the "filter" so that only available fields > will appear in the second div? Shoud I place it in the controller, as > a condition somehow, and how? Or should I use some array-function to > "pop out" the already associated fields from the @fields array? Wich > is better from the point of view of performance, and how do I do each > of them? > > Thanks a lot in advice, for any help > Samuel Brandão > w...-z6zVtI1jGl+AeuBi4663418n826c+h2f@public.gmane.org-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Samuel
2010-Jun-03 14:54 UTC
Re: Has many_through, how to show only unassociated fields for a category?
Hi Sharagoz, thank you for your reply. However, I had already tried array subtraction once, but with no success. I tried for example: associatedFields = @category.fields allFields = Field.all available = allFields - associatedFields however it did not work. When I try: associatedFields.inspect>> [#, #, #, #]allFields.inspect>> [#, #, #, #, #, #, #, #]It seems to me that those resulting variables (allFields and associatedFields) are not of the specific class Array, or if they are, the subtraction is not working properly. I''m gonna do some more tests, to make sure i''m not screwing somewhere else. thanks in advice for any help (at least I know i''m in the right direction :D) Is there ANY way to make this subtraction/selection through SQL queries? On 3 jun, 09:34, Sharagoz <shara...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Fields.all gives you an array of all the fields in the database > @category.fields gives you an array of all the fields for the given > category (since you have the has_many :fields, :through > => :categoryFields association) > > How do you get the fields that are present in the first array but not > in the second? Subtraction. > > @fields_not_chosen_yet = Fields.all - @category.fields > > Btw, you may want to exchange :categoryFields for :category_fields. > > On Jun 2, 5:49 pm, Samuel <gb.sam...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi everybody, > > > I''m using ruby 1.9.1, with rails 2.3.*. I have an application of > > products and categories, but I want each type of product to have > > specific attributes. So, basically, according to the chosen category, > > I want different fields to be shown for the user. > > > So, before actually making the association of the product with the > > attribute, I want to create an administration system where the > > administrators could actually select wich fields they want for each > > category, in an N:N association, usign has many through > > (categoryFields is the middle table). So, up to now, the structure is: > > > class Field < ActiveRecord::Base > > has_many :categoryFields, :dependent => :destroy > > has_many :categories, :through => :categoryFields > > end > > > class CategoryField < ActiveRecord::Base > > belongs_to :field > > belongs_to :category > > end > > > class Category < ActiveRecord::Base > > has_many :categoryFields, :dependent => :destroy > > has_many :fields, :through => :categoryFields > > end > > > And this works perfectly. > > so, now, in my Category Edit view, I want to show a list of ALL > > AVAILABLE FIELDS (i.e. only those not already associated to that > > specific category) at the right, and in the left, all the ASSOCIATED > > FIELDS for that category. Using a simple drag-and-drop, the user can > > drag fields from the right to insert them in category, or move them to > > the right to remove them from that specific category. > > > The problem is that I don''t want that any field in the left column > > also appear in the right column (each field can only be used once in > > that specific category, since those attributes are unique). So, once > > you have associated a field to a category, it will not appear in the > > list of available fields (right list). I''m coming from PHP and this is > > my first rails application (I know, it''s kinda complicated for a > > noob). > > > So, what should I do, to exclude those values? > > > The edit method in the category controller: > > @category = Category.find(params[:id]) > > @categories = Category.roots #the categories acts as nested set, so > > we have to fetch all available parent categories > > @fields = Field.all > > > Now, the view do the following: > > <div class="category_fields span-12 last"> > > <% if !...@category.fields.empty? # this loops through all teh > > ASSOCIATED fields and display their names%> > > <% @category.fields.each do |field| %> > > <%=field.name%> > > <% end %> > > <% end %> > > </div> > > <div class="fields_available"> > > <% if !...@fields.empty? # this loops through ALL THE fields and > > display their names %> > > <% @fields.each do |field| %> > > <%=field.name%> > > <% end %> > > <% end %> > > </div> > > > So, where should I place the "filter" so that only available fields > > will appear in the second div? Shoud I place it in the controller, as > > a condition somehow, and how? Or should I use some array-function to > > "pop out" the already associated fields from the @fields array? Wich > > is better from the point of view of performance, and how do I do each > > of them? > > > Thanks a lot in advice, for any help > > Samuel Brandão > > w...-z6zVtI1jGl+AeuBi4663418n826c+h2f@public.gmane.org-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ivan Nastyukhin
2010-Jun-03 14:56 UTC
Re: Re: Has many_through, how to show only unassociated fields for a category?
> > associatedFields = @category.fields > allFields = Field.all > > available = allFields - associatedFieldsField.all :conditions => ["id not in (?)", @category.fields.all.map(&:id)] Field.where(["id not in (?)", @category.fields.all.map(&:id)]) if rails 3 Ivan Nastyukhin dieinzige-BUHhN+a2lJ4@public.gmane.org On Jun 3, 2010, at 6:54 PM, Samuel wrote:> Hi Sharagoz, thank you for your reply. However, I had already tried > array subtraction once, but with no success. > > I tried for example: > > associatedFields = @category.fields > allFields = Field.all > > available = allFields - associatedFields > > however it did not work. > When I try: > > associatedFields.inspect >>> [#, #, #, #] > > allFields.inspect >>> [#, #, #, #, #, #, #, #] > > It seems to me that those resulting variables (allFields and > associatedFields) are not of the specific class Array, or if they are, > the subtraction is not working properly. I''m gonna do some more tests, > to make sure i''m not screwing somewhere else. > > thanks in advice for any help (at least I know i''m in the right > direction :D) > > Is there ANY way to make this subtraction/selection through SQL > queries? > > > > > > > > > On 3 jun, 09:34, Sharagoz <shara...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Fields.all gives you an array of all the fields in the database >> @category.fields gives you an array of all the fields for the given >> category (since you have the has_many :fields, :through >> => :categoryFields association) >> >> How do you get the fields that are present in the first array but not >> in the second? Subtraction. >> >> @fields_not_chosen_yet = Fields.all - @category.fields >> >> Btw, you may want to exchange :categoryFields for :category_fields. >> >> On Jun 2, 5:49 pm, Samuel <gb.sam...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >> >>> Hi everybody, >> >>> I''m using ruby 1.9.1, with rails 2.3.*. I have an application of >>> products and categories, but I want each type of product to have >>> specific attributes. So, basically, according to the chosen category, >>> I want different fields to be shown for the user. >> >>> So, before actually making the association of the product with the >>> attribute, I want to create an administration system where the >>> administrators could actually select wich fields they want for each >>> category, in an N:N association, usign has many through >>> (categoryFields is the middle table). So, up to now, the structure is: >> >>> class Field < ActiveRecord::Base >>> has_many :categoryFields, :dependent => :destroy >>> has_many :categories, :through => :categoryFields >>> end >> >>> class CategoryField < ActiveRecord::Base >>> belongs_to :field >>> belongs_to :category >>> end >> >>> class Category < ActiveRecord::Base >>> has_many :categoryFields, :dependent => :destroy >>> has_many :fields, :through => :categoryFields >>> end >> >>> And this works perfectly. >>> so, now, in my Category Edit view, I want to show a list of ALL >>> AVAILABLE FIELDS (i.e. only those not already associated to that >>> specific category) at the right, and in the left, all the ASSOCIATED >>> FIELDS for that category. Using a simple drag-and-drop, the user can >>> drag fields from the right to insert them in category, or move them to >>> the right to remove them from that specific category. >> >>> The problem is that I don''t want that any field in the left column >>> also appear in the right column (each field can only be used once in >>> that specific category, since those attributes are unique). So, once >>> you have associated a field to a category, it will not appear in the >>> list of available fields (right list). I''m coming from PHP and this is >>> my first rails application (I know, it''s kinda complicated for a >>> noob). >> >>> So, what should I do, to exclude those values? >> >>> The edit method in the category controller: >>> @category = Category.find(params[:id]) >>> @categories = Category.roots #the categories acts as nested set, so >>> we have to fetch all available parent categories >>> @fields = Field.all >> >>> Now, the view do the following: >>> <div class="category_fields span-12 last"> >>> <% if !...@category.fields.empty? # this loops through all teh >>> ASSOCIATED fields and display their names%> >>> <% @category.fields.each do |field| %> >>> <%=field.name%> >>> <% end %> >>> <% end %> >>> </div> >>> <div class="fields_available"> >>> <% if !...@fields.empty? # this loops through ALL THE fields and >>> display their names %> >>> <% @fields.each do |field| %> >>> <%=field.name%> >>> <% end %> >>> <% end %> >>> </div> >> >>> So, where should I place the "filter" so that only available fields >>> will appear in the second div? Shoud I place it in the controller, as >>> a condition somehow, and how? Or should I use some array-function to >>> "pop out" the already associated fields from the @fields array? Wich >>> is better from the point of view of performance, and how do I do each >>> of them? >> >>> Thanks a lot in advice, for any help >>> Samuel Brandão >>> w...-z6zVtI1jGl+AeuBi4663418n826c+h2f@public.gmane.org > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.