ruby on rails 3.2 - Active Record Association without using id -


i not sure hasn't been answered, not sure search please point me in right direction if asking answered.

i have 2 models:

stock_symbol , weight_symbol

stock_symbol has symbol in matches commodity in weight_symbol model

how can association work when stock_symbol.weight_symbol, weight_symbol back.

i know how in sql, if not standard id this_id lost.

edit:

class stocksymbol < activerecord::base   has_many :weight_symbols, primary_key: :symbol    def commodity     "lc" # example simplify it, there more this.   end end  class weightsymbol < activerecord::base   belongs_to :stock_symbol, foreign_key: :commodity end 

sample object stock_symbol:

stocksymbol.last <#stocksymbol id: 729, symbol: "lcj13c12500", created_at: "2013-03-15 21:50:49", updated_at: "2013-03-15 21:50:49"> 

sample object weight_symbol:

weightsymbol.first <#weightsymbol id:1, weight_group_id: 1, symbol: "lc", created_at: "2010-01-05 21:13:28", updated_at: "2010-01-05 21:13:28"> 

after setting , running stocksymbol.last.weight_symbols.to_sql get:

"select `weight_symbols`.* `weight_symbols` `weight_symbols`.`stock_symbol_id` = 'lcj13c12500'" 

edit 2:

query should doing (i think):

select * `weight_symbols` `weight_symbols`.`symbol` = 'lc'; 

query info want.

select * `weight_symbols` join `stock_symbols` b on a.symbol = b.commodity b.symbol = 'lcj13c12500'; 

new stock symbol object

<#stocksymbol id: 729, symbol: "lcj13c12500", created_at: "2013-03-15 21:50:49", updated_at: "2013-03-15 21:50:49", commodity: "lc"> 

the rails association methods come options override default keys used in relationship. if it's 1 many association between stocksymbol , weightsymbol, use following:

# stock_symbol.rb has_many :weight_symbols, primary_key: :symbol  # weight_symbol.rb belongs_to :stock_symbol, foreign_key: :commodity 

(not sure if got association right way around, should idea.)

the detailed association reference in association basics guid has thorough listing of options , methods belonging each type of association.


edit: stuff based on updated question.

i'm little confused on how association supposed work -- weightsymbol doesn't seem have commodity attribute. should weightsymbol matched owning stocksymbol when weight_symbol.symbol == stock_symbol.commodity? in case, need set keys accordingly:

# stock_symbol.rb has_many :weight_symbols, primary_key: :commodity # :primary_key field on model value associated models store in foreign_key  # weight_symbol.rb belongs_to :stock_symbol, foreign_key: :symbol # :foreign_key column on model should match primary_key field in associated model 

secondly, can set rails associations using database columns, cannot use method. if absolutely must use method, fake association, example using like:

# stock_symbol.rb def weight_symbol   weightsymbol.find_by_symbol(self.commodity) end 

the downside of horribly inefficient, , there lot of reinventing wheel involved.

if have option make commodity database field, possibly best solution, depending on how it's change. example, if it's derived current state of model, set before_save filter on model whatever processing needs happen , write appropriate value field:

# stock_symbol.rb before_save :set_commodity  def set_commodity   commodity = "lc" # arrived @ through whatever processing   self.commodity = commodity end 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -