thx a lot rick, for taking you time to answer to my problem
yeah, it was a typo it was line_items, instead of list_items.
but...still don'tget something, i'm a rails noob what would you
what does rails exactly do when you write li.product =
I don't know how deep you want to get but.
Each association in an active record model is represented by some form
of association proxy. the
declaration in LineItem dynamically defines two methods, I'm going to
use a mix of pseudo-code and 'real' code which is not exactly what's
there but should be a bit clearer, for details just read the
ActiveRecord code in
lib/associations.rb, and lib/associations/belongs_to_association
# code to get the association proxy for the product association,
which will be in this case an instance of BelongsToAssociation
association = product # Get the association
And the important bit of BelongsToAssociation#replace looks like this:
# code to deal with counter caching omitted ...
# more counter cache code omitted
@target = @owner[@reflection.primary_key_name] = nil
# More counter cache code omitted
# The next line sets @target (which is the ActiveRecord
object which the record value belongs to
# based on the value on the rhs of the 'assignment' which
is the parameter record.
# The conditional expression deals with the fact that
# the value could be either another proxy, as it happens to
be in the case you asked about
# product = CartItem.product
# or just a plain model object as it would be in eg.
# product = Product.new(...)
@target = (AssociationProxy === record ? record.target : record)
# Now we set the foreign key field, but only if the value is
already saved and therefore has a primary key.
# the primary_key_name and record_id methods deal get the
primary key names for the association, and the value
# which may be affected by the :foreign_key option.
# The @owner instance variable refers to the model instance
which has this BelongsToAssociation,
# which in this case is a LineItem
@owner[@reflection.primary_key_name] = record_id(record)
# The next line marks the association as dirty, so that it
will be updated when the record is saved.
@updated = true
# this marks the association as being loaded, so that the read
accessor knows not to do a SQL request
There is also some magic I won't go into which handles cases like this
li.product = Product.new()
# Note tthis does not save the new product, so it doesn't have an id
yet, so li.product_id can't be set, until
Which causes the Product referenced by the belongs_to association to
be saved so that the product_id field in the lineitem can be saved.
and wouldn't it be more logical to write something like li.product_id
= cart_item.product_id ???
While that's certainly possible, it isn't using AR to its value as an
Object Relational Mapper. The whole point of AR is to let you think
of models as Ruby objects rather than database tables. While you can
get closer to whats happening in the SQL, even going so far as writing
custom SQL statements, that should be done as a last resort IMHO,
although I must admit that using product_id vs. product can be useful
in some cases, and is just a small step down that slippery slope. <G>