I'm a beginner to both Ruby and Rails and I'm currently reading the Agile
development with Rails, in which I'm currently developing the cart of the
store.

I have a *line_items* model which *belongs_to :products*. This makes sense.
Later in the example we use this code to check if a product is referenced
by any line items before we destroy it:

class Product < ActiveRecord::Base

has_many :line_items

before_destroy :ensure_not_referenced_by_any_line_item

private

# ensure that there are no line items referencing this product

def ensure_not_referenced_by_any_line_item

if line_items.empty?

return true

else

errors.add(:base, 'Line Items present')

return false

end

end

end


This makes sense to me except of one part: if line_items.empty? I can only guess that *line_items *returns all the rows of the "line_items" table that contain the product.id of the currently instantiated Product object, is that right? But how does the model knows what to fetch just by "line_items"? Isn't that too little info that we give to our model, regarding the logic of the task it has to do? Don't we have to declare somewhere something like: *return false if line_items.product.id == product.id* ?


Thanks in advance people!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/4LQoGwJO8J8J.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
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.

Search Discussions

  • Colin Law at Jan 17, 2012 at 11:57 am

    On 17 January 2012 11:47, Agis A. wrote:
    I'm a beginner to both Ruby and Rails and I'm currently reading the Agile
    development with Rails, in which I'm currently developing the cart of the
    store.

    I have a line_items model which belongs_to :products. This makes sense.
    Later in the example we use this code to check if a product is referenced by
    any line items before we destroy it:

    class Product < ActiveRecord::Base

    has_many :line_items

    before_destroy :ensure_not_referenced_by_any_line_item

    private

    # ensure that there are no line items referencing this product

    def ensure_not_referenced_by_any_line_item

    if line_items.empty?

    return true

    else

    errors.add(:base, 'Line Items present')

    return false

    end

    end

    end


    This makes sense to me except of one part: if line_items.empty? I can only
    guess that line_items returns all the rows of the "line_items" table that
    contain the product.id of the currently instantiated Product object, is that
    right? But how does the model knows what to fetch just by "line_items"?
    Isn't that too little info that we give to our model, regarding the logic of
    the task it has to do? Don't we have to declare somewhere something like:
    return false if line_items.product.id == product.id ?
    The fact that you have said has_many :line_items automatically makes a
    method line_items available for any product that returns an array
    (actually it is not strictly an array, but near enough) containing all
    the line items for that product.

    Similarly if you have a line item in @line_item then you can say
    @line_item.product to get the associated product.

    Have a look at the Rails Guide on ActiveRecord associations to find
    all the methods that rails makes available.

    Colin

    --
    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@googlegroups.com.
    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.
  • Agis A. at Jan 17, 2012 at 12:05 pm
    I see.

    Now I've upgraded my seeds.rb file, to add some entry into the line_items
    table and see how it works.
    Actually I've added this line: *LineItem.create (product_id: '8', cart_id:
    '1')*
    *
    *
    but when I run rake db:seed I get this error:

    rake db:seed
    rake aborted!
    /rails/depot/db/seeds.rb:55: syntax error, unexpected tLABEL
    LineItem.create (product_id: 8, cart_id: 1)
    ^

    My migration file for LineItems table is this:
    class CreateLineItems < ActiveRecord::Migration
    def change
    create_table :line_items do |t|
    t.integer :product_id
    t.integer :cart_id
    t.timestamps
    end
    end
    end
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/f4XlY7oLa7gJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    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.
  • Peter Vandenabeele at Jan 17, 2012 at 12:08 pm

    On Tue, Jan 17, 2012 at 1:05 PM, Agis A. wrote:

    I see.

    Now I've upgraded my seeds.rb file, to add some entry into the line_items
    table and see how it works.
    Actually I've added this line: *LineItem.create (product_id: '8',
    cart_id: '1')*
    *
    *
    but when I run rake db:seed I get this error:

    rake db:seed
    rake aborted!
    /rails/depot/db/seeds.rb:55: syntax error, unexpected tLABEL
    LineItem.create (product_id: 8, cart_id: 1)
    Remove the space before the '('

    LineItem.create(product_id: 8, cart_id: 1)

    In newer versions of Ruby (1.9.3 and maybe earlier), this is not longer
    allowed.

    Peter

    --
    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@googlegroups.com.
    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.
  • Agis A. at Jan 17, 2012 at 12:30 pm
    Thank you! Silly me for missing that.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/PWJe97NuBPkJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 17, '12 at 11:48a
activeJan 17, '12 at 12:30p
posts5
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase