Displaying 20 results from an estimated 1000 matches similar to: "Prevent duplicate HABTM associations"
2006 May 01
4
Find rows with associations in habtm
This may have been asked/answered before - if so, i apologise...
I have the following table layout in a habtm relationship:
------------ ----------------------- --------------
| products | ----| categories_products |-----| categories |
------------ ----------------------- --------------
What would be the best way to pull out all the products for a given
category? At the
2006 Jun 05
5
adding habtm through migrations
Hello,
I''m stuck, I''ve got a model Product and would like to add a new model
called Category. Furthermore, I want to set up a many2many
relationship between the mentioned models. the problem is that I do
not know how to set the primary key in the categories_products table.
this is what I''ve got in my migration file:
create_table :categories do |t|
t.column :name,
2005 Jun 03
1
sorting on a habtm relationship
I have two tables ("products" and "categories") that have a
has_and_belongs_to_many relationship, and I need to be able to
arbitrarily sort products in the scope of a category using something
like acts_as_list. Problem is, since each product can belong to
multiple categories, it doesn''t work to use acts_as_list and have the
"position" field be in the
2006 Jun 27
4
Not Active Record Model Validation
I have a problem with ruby on rails validation
total_book_toy.rhtml
================
<%= text_field ''book1'', ''title1'' %>
<%= text_field ''book2'', ''title2'' %>
I want to validate these text_field so user can''t insert same title.
However, I was stuck how to do it.
Or maybe you have another way how to do it.
2006 Nov 29
2
validates_uniqueness w scope
In a one-to-many relationship (domain has_many :categories and
category belongs_to :domain)
In the category model I use:
validates_uniqueness_of :name, :case_sensitive => false
but upon creation of a category this validation is performed on ALL
domains,
can I restrict it only to the categories of the current domain, the to
which teh category being saved belongs_to ? if yes ? how ?
thanks
2010 Jul 12
1
How to turn :before_add exceptions into validation errors
As per http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
Possible callbacks are: before_add, after_add, before_remove and
after_remove.
Should any of the before_add callbacks throw an exception, the
object does not get added to the collection. Same with the
before_remove callbacks; if an exception is thrown the object doesnât
get removed.
However, when the add
2006 Jul 30
1
Doubt on composite index
The AWDwR book has the following:
add_index :categories_products, [:product_id, :category_id]
add_index :categories_products, :category_id
The first, composite index actually serves two purposes: it creates an index which can be searched
on both foreign key columns, and with most databases it also creates an
index that enables fast lookup by the product id. The second index then
completes
2006 Jul 18
3
Newbie validation question
I am having problems with a validation, the full model code is below.
The validate routine is attempting to see if another record with the
same artist_name, title, & catalogue_no exists in the products table. If
so, it should throw an error.
This is the validation code:
def validate
if Product.find(:all, :conditions => ["artist_name = ?, title = ?,
catalogue_no = ?",
2006 Jul 07
2
many-to-many error
hey all. I''m trying to set up a many-to-many relationship with products
and categories.
I''ve got the product model (product.rb) set up:
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
end
and a similar one for category.rb
And I''ve got the tables set up -- products, categories,
categories_products
But when I load, I get: undefined method
2006 May 28
7
Join table not being populated
Hello,
I have the usual Categories and Products table scenario, mediated by a
Categories_Products join table. When I create a new Product and into a
given Category, I notice that my join table is not being populated.
Shouldn''t the join Categories_Products table be populated?
Thanks,
gk
--
Posted via http://www.ruby-forum.com/.
2006 Oct 12
2
How do I test a unique index?
I have a loans table and a payments table. Each loan has_many payments.
So far no problem.
Now, the payments table has a payment_number field, and each payment has
a payment_number unique within that loan. For example, Loan A can have
payment_numbers 0, 1, and 2 and Loan B can have payment_numbers 0, 1, 2,
and 3 (but only one of each payment_number). I can enforce this with a
unique index in
2006 Apr 05
4
validation on before_add callback
problem: validation error is not functionning in callback
callback for many-to-many association:
# keyword.rb
:before_add => :reject_self_related
def reject_self_related(related_keyword)
if (related_keyword == self)
# this does not work:
errors.add(:name, "Relating a keyword to itself is
forbidden!")
raise "Relating a keyword to itself is
2006 Jul 19
2
Creating a safe sequence generator
Hi all,
I''ve got a rails and database question. Accounts have jobs, and job
numbers should be assigned per account. Account 89 should have jobs
1-whatever, which are not the same as account 67, which has jobs
1-whatever.
In the past, I have accomplished this quite easily with:
create table job (
accountid int(10) unsigned not null default 0,
jobid int(10) unsigned not null
2006 Jun 13
1
AJAX based record deletion
Hi,
New to RoR and I just wanted to check the correct process flow for
this...
I presently have a controller action:
def destroy_category_association
category = CatalogueCategory.find(params[:id])
product = Product.find(params[:product_id])
product.catalogue_categories.delete(category)
end
I display a list of categories assigned to a product, some of which I
may wish to delete
2006 Jul 27
1
importing data with migration for habtm
I have 2 tables: products and categories. They are both linked by a
habtm association. I have a fixture to import all the data in each table
but I dont know how to import the data in the categories_products table
since that table has no model, so I cant use that code:
f = Fixtures.new(Category.connection, # a database connection
"categories", # table name
2006 Apr 30
1
Callbacks on has_many relation
Hello,
Is there a way to implement a callback for somehting like before_add and
after_ad on a statement like this with a has_many relation?
x.children << Item.create(:title=>"test")
I want that x has a callback that is called when the item is added.
I found something in
2006 Apr 05
2
validates_uniqueness_of with habtm
it''s a blog...
class Entry < ActiveRecord::Base
has_and_belongs_to_many :categories
end
similarly:
class Category < ActiveRecord::Base
has_and_belongs_to_many :entries
end
I wish to make sure that the owner doesn''t add the same category to a
given entry more than once... I''m thinking that validates_uniqueness_of
should be usable here. But can''t
2006 May 08
8
enforcing special behavior of child rows in HABTM
Hi,
I have a scenario where a doctor can have one or more specialties.
For each doctor, one and only one of her specialties can be designated
as primary.
So I have tables called doctors, specialties, and doctors_specialties,
the last of which has a boolean is_primary column.
The doctor model class specifies that:
has_and_belongs_to_many :specialties
I want to enforce, at the lowest possible
2006 May 03
3
Sorting by multiple categories
I have a database with a field called ''category''.
''category'' contains the categories separated by a "," i.e. 1, 2, 5
I know with something like:
@products = Product.find(:all, :condition => "category = ''1''")
I could get some results if any of the ''category'' fields only contained
"1" but
2011 Jan 14
0
[Rail3] How to construct full-text search for habtm associations
hi all, i''m new to this group and currently using Rails 3.0.3 and meta_where
0.9.10.
i''m considering how to make a full-text searching feature to my project,
and my models, for example, should be defined like below:
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
# :title (string)
end
class Comment < ActiveRecord::Base
has_and_belongs_to_many :users