婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識(shí)庫 > Ruby on Rails中的ActiveRecord編程指南

Ruby on Rails中的ActiveRecord編程指南

熱門標(biāo)簽:百度地圖標(biāo)注自定義圖片 電銷機(jī)器人廠商代理 徐州網(wǎng)絡(luò)外呼系統(tǒng)哪個(gè)好 白銀外呼paas系統(tǒng) 常德電銷平臺(tái)外呼系統(tǒng)軟件價(jià)格 高德地圖標(biāo)注客服 滴滴外呼系統(tǒng) 湖州u友防封電銷卡 地圖標(biāo)注賺錢項(xiàng)目注冊(cè)


    避免改動(dòng)缺省的 ActiveRecord(表的名字、主鍵,等等),除非你有一個(gè)非常好的理由(像是不受你控制的數(shù)據(jù)庫)。
    把宏風(fēng)格的方法放在類別定義的前面(has_many, validates, 等等)。

    偏好 has_many :through 勝于 has_and_belongs_to_many。 使用 has_many :through 允許在 join 模型有附加的屬性及驗(yàn)證

   

 # 使用 has_and_belongs_to_many
  class User  ActiveRecord::Base
   has_and_belongs_to_many :groups
  end

  class Group  ActiveRecord::Base
   has_and_belongs_to_many :users
  end

  # 偏好方式 - using has_many :through
  class User  ActiveRecord::Base
   has_many :memberships
   has_many :groups, through: :memberships
  end

  class Membership  ActiveRecord::Base
   belongs_to :user
   belongs_to :group
  end

  class Group  ActiveRecord::Base
   has_many :memberships
   has_many :users, through: :memberships
  end

    使用新的 "sexy" validation。

    當(dāng)一個(gè)慣用的驗(yàn)證使用超過一次或驗(yàn)證是某個(gè)正則表達(dá)映射時(shí),創(chuàng)建一個(gè)慣用的 validator 文件。

  # 差
  class Person
   validates :email, format: { with: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
  end

  # 好
  class EmailValidator  ActiveModel::EachValidator
   def validate_each(record, attribute, value)
    record.errors[attribute]  (options[:message] || 'is not a valid email') unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
   end
  end

  class Person
   validates :email, email: true
  end

    所有慣用的驗(yàn)證器應(yīng)放在一個(gè)共享的 gem 。

    自由地使用命名的作用域(scope)。

   

 class User  ActiveRecord::Base
   scope :active, -> { where(active: true) }
   scope :inactive, -> { where(active: false) }

   scope :with_orders, -> { joins(:orders).select('distinct(users.id)') }
  end

    將命名的作用域包在 lambda 里來惰性地初始化。

 

  # 差勁
  class User  ActiveRecord::Base
   scope :active, where(active: true)
   scope :inactive, where(active: false)

   scope :with_orders, joins(:orders).select('distinct(users.id)')
  end

  # 好
  class User  ActiveRecord::Base
   scope :active, -> { where(active: true) }
   scope :inactive, -> { where(active: false) }

   scope :with_orders, -> { joins(:orders).select('distinct(users.id)') }
  end

    當(dāng)一個(gè)由 lambda 及參數(shù)定義的作用域變得過于復(fù)雜時(shí),更好的方式是建一個(gè)作為同樣用途的類別方法,并返回一個(gè) ActiveRecord::Relation 對(duì)象。你也可以這么定義出更精簡的作用域。

  class User  ActiveRecord::Base
   def self.with_orders
    joins(:orders).select('distinct(users.id)')
   end
  end

    注意 update_attribute 方法的行為。它不運(yùn)行模型驗(yàn)證(不同于 update_attributes )并且可能把模型狀態(tài)給搞砸。

    使用用戶友好的網(wǎng)址。在網(wǎng)址顯示具描述性的模型屬性,而不只是 id 。
    有不止一種方法可以達(dá)成:

        覆寫模型的 to_param 方法。這是 Rails 用來給對(duì)象建構(gòu)網(wǎng)址的方法。缺省的實(shí)作會(huì)以字串形式返回該 id 的記錄。它可被另一個(gè)具人類可讀的屬性覆寫。

    class Person
     def to_param
      "#{id} #{name}".parameterize
     end
    end

    為了要轉(zhuǎn)換成對(duì)網(wǎng)址友好 (URL-friendly)的數(shù)值,字串應(yīng)當(dāng)調(diào)用 parameterize 。 對(duì)象的 id 要放在開頭,以便給 ActiveRecord 的 find 方法查找。
    * 使用此 friendly_id gem。它允許藉由某些具描述性的模型屬性,而不是用 id 來創(chuàng)建人類可讀的網(wǎng)址。

  Ruby
  class Person
  extend FriendlyId
  friendly_id :name, use: :slugged
  end

    查看 gem 文檔獲得更多關(guān)于使用的信息。

您可能感興趣的文章:
  • 關(guān)于Ruby on Rails路由配置的一些建議
  • 快速安裝Ruby on Rails的簡明指南
  • 關(guān)于Ruby on Rails視圖編寫的一些建議

標(biāo)簽:普洱 遼寧 永州 公主嶺 張家界 梧州 荊門 三沙

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby on Rails中的ActiveRecord編程指南》,本文關(guān)鍵詞  Ruby,Rails,中的,ActiveRecord,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Ruby on Rails中的ActiveRecord編程指南》相關(guān)的同類信息!
  • 本頁收集關(guān)于Ruby on Rails中的ActiveRecord編程指南的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 肥城市| 木兰县| 勐海县| 崇左市| 承德县| 武川县| 凤阳县| 汝阳县| 屏南县| 台安县| 乡城县| 余庆县| 湘乡市| 碌曲县| 保德县| 临猗县| 房山区| 峨眉山市| 习水县| 宁城县| 改则县| 神池县| 清水河县| 屏边| 手游| 马山县| 乌海市| 五峰| 蒙城县| 佛冈县| 绥芬河市| 吴堡县| 临城县| 雷山县| 大兴区| 灵武市| 英山县| 德惠市| 普陀区| 特克斯县| 松阳县|