开发者

Rails Associtions

开发者 https://www.devze.com 2023-03-18 15:39 出处:网络
Firm has_many :clients Client belongs_to :firm # w Forign key firm_id I can do client.firm.name my question:

Firm has_many :clients

Client belongs_to :firm # w Forign key firm_id

I can do client.firm.name

my question:

what is the best way to find clien开发者_Go百科ts that belong to this firm in the same format?


To get all of the client objects, just do...

c = firm.clients

To get all of the client names for a firm as an array of strings, do...

names = firm.clients.all.collect {|c| c.name}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号