abou ihsaan wrote:> I have 3 models:
>
> - Section (habtm :Category)
> - Grouped (belongs_to :Section/:Category)
> - Category (habtm :Section)
Can you post the full model definition code? Getting a shorthand version
isn''t
always helpful. Also it looks like you have used "category" and
"section",
rather than "categories" and "sections" in your
has_and_belongs_to_many
statements. You might want to change that, unless your plan is to not follow
Rails'' standard pluralization.
> When I try to attach the section, with the same category like:
>
> @section = Section.find(:first, :include => :category )
> @section.name = ''hoi''
> @section.category.push Category.find(:first)
>
> I get a error: "Duplicate entry ''1'' for key 1:
INSERT INTO
> categories_sections (`section_id`, `id`, `category_id`) VALUES (1, 1,
> 1)"
I see a couple of things "off" with what you are trying to do here.
First, your
join table: categories_sections really shouldn''t have an ID column.
Join tables
don''t need IDs since a compound field of the two foreign keys should
always be
unique.
Second, it looks like you''re trying to add the same category, multiple
times, to
a single section. That''s not how many-to-many relations work. Rails
isn''t giving
you errors there, your Database is giving you errors.
> I know there is a feature freeze of rails 1.0, but this is quite
> anooying, becuase I only want one entry for each join (between
> section/category) and would like to push more info in the join table and
> update it.
I''m not sure what the Rails 1.0 feature freeze has to do with
anything... But
there are examples all over of having extra attributes in a join table.
I''ve
never needed to use them, myself, so I can''t really speak from
experience here...
> But when I do I still get the same error:
>
> @sections = Section.find(:all, :include => :category )
>
> @section = Section.find(:first, :include => :category )
> @section.name = ''hoi''
> @section.category.push_with_attributes Category.find(:first), :status =>
> ''hidden''
I don''t know much about push_with_attributes, but it looks to me like
you are
doing way too much "finding" in these statements. First you find all
sections,
with their associated categories, then you do another find for the first
session, which is already contained in @sessions[1].
Then you find the "first" category, which could be anything, without
first
checking to see if the first category is already attached to the first section.
I''m pretty sure Rails will do what you need, but I''m not sure
you are
approaching it the right way.
> Looks like there is no way of updating the join? rails is very fine on
> all aspect so I didn''t expect this to be a problem, but I
can''t find
> much about it expect a few time''s mentioning several bugs...
I''m sure there''s a method for updating a join, however, as
I''ve already stated,
that''s not something I''ve done before. But you may want to
look into
update_attributes, rather than push_with_attributes, for a join that''s
already
been established. The push operation is trying to both establish a new join and
update the extra attribute(s). That doesn''t sound like what you are
trying to do.
> To be clear, I am searching for something to enforce uniq id''s in
the
> join table and to access/update the extra data contained in the join
> table
You don''t need unique id''s in the join table. The foreign keys
from the related
tables already provide all the uniqueness you should need. At least, for a
standard many-to-many relation. If you need more than that, you might want to
break out your join into it''s own model and use has_many/belongs_to
from the
previous habtm models.
> last question about this topic: Is there a way to access the join model?
> something like Section.grouped?
Without a look at your model definition code, I''m not sure this
question is
answerable. This is the first time you referenced "grouped" in your
post, other
than mentioning it was a model up at the top.
-Brian