SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rails: Set default model values

    Hi,

    I have a model called Product. The Product has date_modified and date_created fields. I'd like to set the value to now() (mysql). I thought it'd be as simple as setting the value in the controller.create method:

    @product = Product.new(params[roduct])
    @product.date_created = 'now()'

    The 'now()' value needs to be un-escaped: values(now(), 'string', etc..)

    But that doesn't do it. What gives? Also, is there a way to do this in the model?

    -matt

  2. #2
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The easy way to get this behaviour is to take advantage of the Timestamp module and rename your column from date_created to created_on.

    The manual way to do it, would be more like this:

    Code:
    @product = Product.new(params[:product])
    @product.date_created = Time.now
    You can see the source for the created_on feature here, and you can see it does basically the same thing as the code above.

    Edit: if you really want to use 'now()', you could probably do it with connection.execute(sql),though it isn't sometihng I've had to use before.

    hth,
    Douglas
    Hello World

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I concur. Its probably not a very smart idea to use direct SQL since rails will handle escaping the values for you. Using the Time module you can create timestamps that work with SQL databases

  4. #4
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK cool. But just for my own knowledge, how do you set a models values directly? Without quoting?

    Thanks a lot! It's working great using Time.now

    -matt

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you name your columns "created_on" and "modified_on" then Rails will handle this for you automatically.

  6. #6
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mwmitchell
    OK cool. But just for my own knowledge, how do you set a models values directly? Without quoting?
    From outside the model? Don't know, sorry.

    From inside the model, write_attribute/read_attribute are useful, but everything in the attributes hash goes through attributes_with_quotes on create and update (called by my_model_object.save). That means you'll have to write the SQL yourself to get unquoted data in there.

    Here's how Rails does update internally, you could base your code on that:

    Code:
    1435 	      def update
    1436 	        connection.update(
    1437 	          "UPDATE #{self.class.table_name} " +
    1438 	          "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " +
    1439 	          "WHERE #{self.class.primary_key} = #{quote(id)}",
    1440 	          "#{self.class.name} Update"
    1441 	        )
    1442 	      end
    So you'd have something like:

    Code:
    def custom_update
      custom_sql_data = "... now() ..."
      connection.update(
        "UPDATE #{self.class.table_name} " +
        "SET #{custom_sql_data} " +
        "WHERE #{self.class.primary_key} = #{quote(id)}",
        "#{self.class.name} Update"
      )
    end
    And I think it should work. Just remember that you are going around most of ActiveRecord by doing that, so if you find yourself doing it regularly, you might want to use a different ORM, or just not bother with an ORM in the first place

    Douglas
    Hello World

  7. #7
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! I'm going to go with the created_on and modified_on method. So simple.

    -matt


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
  •