SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    grasshoppa Snowbird122's Avatar
    Join Date
    Apr 2001
    Location
    Austin
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    active record newbie here

    I have 2 tables: Products and ProductLines

    Products:
    id
    product_line_id

    ProductLines:
    id
    product_line


    Product Model:
    Code:
         class Product < ActiveRecord::Base
           belongs_to :product_line
         end
    ProductLine Model:
    Code:
         class ProductLine < ActiveRecord::Base
           has_one :product
         end
    In my listing code, I am trying to show the product line for each product like this:
    Code:
         <% for product in @products %>
           <tr><td><%=h product.product_line %></td></tr>
        <% end %>
    I am getting this error:
    DBI: DatabaseError: Execute
    OLE error code:80040E37 in Microsoft OLE DB Provider for SQL Server
    Invalid object name 'product_lines'.
    HRESULT error code:0x80020009
    Exception occurred.: SELECT TOP 1 * FROM product_lines WHERE (product_lines.id = 4)


    Note that it thinks my table is named 'product_lines' when it is really named ProductLines. Also note that I am using MS SQL Server, which should be fine, but is noteworthy.
    http://www.echo-consulting.net - Sound Solutions for Online Inspriations.

  2. #2
    grasshoppa Snowbird122's Avatar
    Join Date
    Apr 2001
    Location
    Austin
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed my ProductLine model to
    Code:
     class ProductLine < ActiveRecord::Base
       has_many :products
     end
    but still get the same error.
    http://www.echo-consulting.net - Sound Solutions for Online Inspriations.

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your table names should be product_lines and products, lowercase with underscores, not camel case. That is the Rails convention. If you really want to keep it as it is, then you need to set the table name in each of your models. E.g.

    Code:
    class ProductLine < AR::Base
      set_table_name 'ProductLines'
    end
    I'd recommend sticking with Rails conventions unless you are forced to do otherwise (for example, working with legacy databases).

  4. #4
    grasshoppa Snowbird122's Avatar
    Join Date
    Apr 2001
    Location
    Austin
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Luke. I changed my table names to products and product_lines, and now I'm not getting the error, but instead, I get a # for allmy product_lines. Any ideas on that?
    http://www.echo-consulting.net - Sound Solutions for Online Inspriations.

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Is the product_line field in your database table a number? You'll probably have to write code to reference another field in your product_line table as the one to display to the user.

  6. #6
    grasshoppa Snowbird122's Avatar
    Join Date
    Apr 2001
    Location
    Austin
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Vinnie. The product line field in the table product_lines is nvarchar(50), so it isn't a number. In the view, I even tried product.product_line.to_s, but I still get the # for every row.
    http://www.echo-consulting.net - Sound Solutions for Online Inspriations.

  7. #7
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post your schema?

  8. #8
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Snowbird122
    Thanks Vinnie. The product line field in the table product_lines is nvarchar(50), so it isn't a number. In the view, I even tried product.product_line.to_s, but I still get the # for every row.
    I think the confusion is that the product_line you are using is an Active Record derived ProductLine object, not just the product_line field in your database.

    Try:
    product.product_line.product_line to get at that value.

    If that works, you can add a custom to_s method in the ProductLine class, so that what you were initially trying will work.

    Code:
         class ProductLine < ActiveRecord::Base
           has_one :product
           def to_s
             self.product_line
           end
         end
    Then you could use products.product_line and maybe get what you want.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  9. #9
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To add to this, I've been playing with rails a bit lately and don't really get how to do relationship mapping with active record. I can't seem to find a decent tutorial that I can wrap my head around it. Maybe im just tring to hard.

    Anyone have a good page or tutorial or something available that points out how to handle relationships? I'd be really greatful

    Thanks,
    Eric
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  10. #10
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  11. #11
    grasshoppa Snowbird122's Avatar
    Join Date
    Apr 2001
    Location
    Austin
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    samsm: That was exactly the problem. When I changed it to product.product_line.product_line, it worked. When I added the to_s function, and changed it back to product.product_line, that worked also.

    I don't really understand why I had this problem. Can you point me to a resource that explains this?

    Luke - thanks for your help also.
    http://www.echo-consulting.net - Sound Solutions for Online Inspriations.

  12. #12
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because:

    Code:
    product.product_line
    Returns the ProductLine associated with the Product. So it delivers an object that represents a row in the database. If you do:

    Code:
    pl = ProductLine.find(1)
    pl.product_line
    pl represents a row in the DB too. If you do pl.product_line, that returns the value of a column of the row: the value of the column product_line in the database.

    So if you do:

    Code:
    product.product_line.product_line
    The first product_line gets you a row, and the second returns a column of that row.

  13. #13
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It might make it clearer if you were to explain what the product_line column holds as I'm sure there must be a better name for it seeing as your model/table is already called product_line.

  14. #14
    grasshoppa Snowbird122's Avatar
    Join Date
    Apr 2001
    Location
    Austin
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fenrir2: I understand, thanks.

    Luke: product_line is the same thing as a category. It is a way to group products. How would you have named the table/field/model differently?
    http://www.echo-consulting.net - Sound Solutions for Online Inspriations.

  15. #15
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Snowbird122
    Fenrir2: I understand, thanks.

    Luke: product_line is the same thing as a category. It is a way to group products. How would you have named the table/field/model differently?
    If your current product_line column in your products table references an ID in the product_lines table, then according to the Rails conventions it should be called product_line_id. That way you don't get confusion like the above

  16. #16
    grasshoppa Snowbird122's Avatar
    Join Date
    Apr 2001
    Location
    Austin
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Vinnie: See my very first post. That is exactly how I did it.
    http://www.echo-consulting.net - Sound Solutions for Online Inspriations.

  17. #17
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm limited, I always seem to stumble back to the same two words when I'm naming fields. "name" and "description". In fact I'm about a few days from starting a thread asking for advice on better names. Who knows, maybe "name" and "description" are fine. They are probably less ambiguous in templates than what you have.


    Code:
    <% for product in products %>
      <%= product.name %>
      <%= product.description %>
      <%= product.product_line.name %>
      <%= product.product_line.description %>
    
    and don't forget that you can...
    
      <%= debug product %>
      <%= debug product.methods %>
      <%= debug product.product_line %>
      <%= debug product.product_line.methods %>
    
    ... to get an idea of what data and methods 
    are available to you with an object.
    <% end %>
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •