how to use associations in view font-end use angularjs and rails 4 -
i using rails 4 write in back-end , angular js write front end. have trouble use associations.
i have 2 models:
class record < activerecord::base has_and_belongs_to_many :tags end class tag < activerecord::base has_and_belongs_to_many :records end
i using model records_tags
connect 2 models record
, tag
.
in controller, write:
def index records = record.all.order('created_at desc') render json: {error:0, data: records} end
in view
<ul> <li ng-repeat ="data in datas"> {{data.tags.name}}</li> </ul>
although, use relationship in controller success, variable datas send view don't use relationship them.
anyone can me?
thats normal, when in controller data
collection on activemodel objects, relations live within it. when export json sending json object. include tags
records
have include tags
in json
object.
def index records = record.all.order('created_at desc') render json: {error:0, data: records.as_json(include: :tags))} end
Comments
Post a Comment