zoukankan      html  css  js  c++  java
  • beginning rails3 note

     

     

    respond_to do |format|

    format.html

    format.xml{render :xml=>@products}

    end

    repsond_to :html,:xml

    respond_with(@products)

    respond_with(@product,location=prudct_url)

    cookies.permanet[:last_product_id]=@product.id

     

    redirect_to [:edit,@product],:notice=>”successfully created product.”

    redirect_to edit_product_path(@product),:noteice=>”successfully created product”

     

     

    Rails is built on the principle of a sared-nothing architecture.

    Shared-nothing means tat no piece of data is sared betwwen te servers tat host the application.

    Storing session data on the server would violate this principle.

    The default cookie-based session storage mechanism “just works” wit no configuration required.

     

    rake db:sessions:create

    rake db:migrate

     

     

    attr_accessor :password

    define an accessor attribute,password,a password method isn’t created automatically by active recored,

    this works like any model attribute ,except that it isn’t persisted to the database wen the model is saved.

     

     

     

    rails g observer Comment

    class CommentObserver < ActiveRecord::Base

        def after_create(comment)

            p “”

        end

    end

     

    before you cann this code in action,you have to tell Rails that you want the CommentServer class to be triggered by the Active Record callbacks of the Comment class.

    To do that,make sure you config/application.rb file looks like bellows & restart your application.

    config.active_record.observers = :comment_observer

     

     

     

     

    class User < ActiveRecord::Base

        has_many :replies,:through=>:articles,:source=>:comments

    end

     

    rails generate migration rename_password_to_hashed_password

    def self.up

    rename_column :users,:password,:hash_passwordend

    end

    rails is not silver bullet!

    scope :where_title,lambda {|term| where(“articles.title like ?”,”%#{term}%”}

    Article.where(“published_at < ? and updated_at > ?”,Time.now,Time.now).to_sql

    Article.where(“title LIKE :searc OR body LIKE :search”,{:search=>”%association%”})

    rake db:setup re-create database and import seed data

    rake db:seed

    db/seeds.rb

    Category.create [{:name=>’programming’},{:name=>’Event’}]

    class User < ActiveRecord::Base

        has_one :profile

        has_many :articles,:order=>’’

        has_many :replies,:through=>:articles,:source=>:comments

    end

    has_one :profile,:class_name=>”Account”,:conditions=>”active=1”,:foreign_key=>”account_id”,:order=>”created_at DESC”,:dependent=>:destroy

    :delete

    :nullify

    user.articles.empty?

    user.articles.clear

    user.article_ids

    return an array of associated aritcle ids.

    user.build_profile(attributes={})

    returns a new profile object that as been instantiated with attributes and linked to user through a foreign key but hasn’t yet been saved

    user.create_profile(attribute={})

    return a new Profile object that as benn instaniated with attributes and linked to user through a foreign key and tat has already been saved

    article.title="Introduction to sql”

    equals

    article.title=(“Introducting to sql”)

    article.new_record?

    article.attributes

    find_by_*(cond)

    find_all_by_*(cond)

    find_by_*_and_*(cond1,cond2)

    find_all_by_*_and_*(cond1,cond2)

    article=Article.first

    article.update_attributes(:title=>”RailsConf”,:published_at=>1.day.ago)

    Article.destroy(4)

    Article.destroy([4,5])

    Article.delete_all

    Article.delete_all(cond)

    article.errors.on(:title)

    article.errors.any?

    article.valid?

    reload!

    reloads the Rails application environment with your console session,

    when you code make changed,you can do this, and it equals re-open rails c

  • 相关阅读:
    sqlserver2000及以上版本导出数据到mysql里的详细图解
    JDBC和ODBC的区别何在?什么是ODBC?
    mysql ODBC 在64位下提示找不到odbc驱动问题
    Microsoft sqlserver2000如何导入.mdf格式的数据库文件
    使用navicat 链接sql server 出现08001错误
    sql语句如何转成hql语句?
    显示所有SAP图标的ABAP代码
    SMW0上传EXCEL模板时报错无分配给对象***的MIME类型
    显示小闹钟的函数
    便携计算器
  • 原文地址:https://www.cnblogs.com/lexus/p/1929090.html
Copyright © 2011-2022 走看看